From 31a806e438696a4982bf837207cc6de689fde3ca Mon Sep 17 00:00:00 2001 From: jfelectron Date: Sun, 23 Nov 2014 11:31:58 -0800 Subject: [PATCH 1/2] add standard file mode param, add content_encoding --- s3file.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/s3file.py b/s3file.py index 890769d..4ff5077 100644 --- a/s3file.py +++ b/s3file.py @@ -14,19 +14,21 @@ def s3open(*args, **kwargs): class S3File(object): - def __init__(self, url, key=None, secret=None, expiration_days=0, private=False, content_type=None, create=True): + def __init__(self, url,mode, key=None, secret=None, expiration_days=0, private=False, content_type=None,content_encoding=None): from boto.s3.connection import S3Connection from boto.s3.key import Key self.url = urlparse(url) self.expiration_days = expiration_days self.buffer = cStringIO.StringIO() - + mode = mode if mode else 'r' self.private = private self.closed = False self._readreq = True self._writereq = False - self.content_type = content_type or mimetypes.guess_type(self.url.path)[0] + mime_type = mimetypes.guess_type(self.url.path) + self.content_type = content_type or mime_type[0] + self.content_encoding = content_encoding or mime_type[1] bucket = self.url.netloc if bucket.endswith('.s3.amazonaws.com'): @@ -36,8 +38,11 @@ def __init__(self, url, key=None, secret=None, expiration_days=0, private=False, self.name = "s3://" + bucket + self.url.path - if create: - self.bucket = self.client.create_bucket(bucket) + if mode =='w': + try: + self.bucket = self.client.create_bucket(bucket) + except: + self.bucket = self.client.get_bucket(bucket, validate=False) else: self.bucket = self.client.get_bucket(bucket, validate=False) @@ -74,6 +79,8 @@ def _remote_write(self): if self.content_type: headers["Content-Type"] = self.content_type + if self.content_encoding: + headers["Content-Type"] = self.content_encoding if self.expiration_days: now = datetime.datetime.utcnow() @@ -136,4 +143,4 @@ def write(self, s): def writelines(self, sequence): self._writereq = True - self.buffer.writelines(sequence) + self.buffer.writelines(sequence) \ No newline at end of file From 381bd16d4f923235afe9129b704a686305256bab Mon Sep 17 00:00:00 2001 From: jfelectron Date: Sun, 23 Nov 2014 15:27:40 -0800 Subject: [PATCH 2/2] set default mode to read --- s3file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s3file.py b/s3file.py index 4ff5077..49b4b82 100644 --- a/s3file.py +++ b/s3file.py @@ -14,7 +14,7 @@ def s3open(*args, **kwargs): class S3File(object): - def __init__(self, url,mode, key=None, secret=None, expiration_days=0, private=False, content_type=None,content_encoding=None): + def __init__(self, url,mode='r', key=None, secret=None, expiration_days=0, private=False, content_type=None,content_encoding=None): from boto.s3.connection import S3Connection from boto.s3.key import Key