Skip to content

Commit

Permalink
S3Boto3 Updates from feedback and catching up to s3boto commits
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarrien committed Feb 12, 2016
1 parent da2a8b1 commit b92f50c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
23 changes: 13 additions & 10 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,9 @@ def __init__(self, name, mode, storage, buffer_size=None):
self.name = name[len(self._storage.location):].lstrip('/')
self._mode = mode
self.obj = storage.bucket.Object(storage._encode_name(name))
# TODO: Is this explicitly necessary? Done to emulate old
# behavior, but the code would misbehave already if self.obj is None.
if 'w' not in mode:
try:
self.obj.load()
except storage.connection_response_error:
self.obj = None
# Force early RAII-style exception if object does not exist
self.obj.load()
self._is_dirty = False
self._file = None
self._multipart = None
Expand Down Expand Up @@ -136,7 +132,7 @@ def read(self, *args, **kwargs):
raise AttributeError("File was not opened in read mode.")
return super(S3Boto3StorageFile, self).read(*args, **kwargs)

def write(self, content, *args, **kwargs):
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was not opened in write mode.")
self._is_dirty = True
Expand All @@ -152,7 +148,7 @@ def write(self, content, *args, **kwargs):
self._multipart = self.obj.initiate_multipart_upload(**parameters)
if self.buffer_size <= self._buffer_file_size:
self._flush_write_buffer()
return super(S3Boto3StorageFile, self).write(force_bytes(content), *args, **kwargs)
return super(S3Boto3StorageFile, self).write(force_bytes(content))

@property
def _buffer_file_size(self):
Expand All @@ -171,8 +167,6 @@ def _flush_write_buffer(self):
self.file.seek(0)
part = self._multipart.Part(self._write_counter)
part.upload(Body=self.file.read())
self.file.close()
self._file = None

def close(self):
if self._is_dirty:
Expand All @@ -187,6 +181,9 @@ def close(self):
else:
if self._multipart is not None:
self._multipart.abort()
if self._file is not None:
self._file.close()
self._file = None


@deconstructible
Expand Down Expand Up @@ -465,6 +462,12 @@ def delete(self, name):
self.bucket.Object(self._encode_name(name)).delete()

def exists(self, name):
if not name:
try:
self.bucket
return True
except ImproperlyConfigured:
return False
name = self._normalize_name(self._clean_name(name))
if self.entries:
return name in self.entries
Expand Down
9 changes: 9 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ def test_storage_open_write(self):
multipart.complete.assert_called_once_with(
MultipartUpload={'Parts': [{'ETag': '123', 'PartNumber': 1}]})

# def test_storage_exists_bucket(self):
# bucket = self.storage._connection.Bucket.return_value
# bucket.meta.client.head_bucket.side_effect = ClientError(
# {'Error': {'Code': 123, 'Message': 'Fake'}}, 'load')
# self.assertFalse(self.storage.exists(''))
#
# self.storage.bucket.meta.client.head_bucket.side_effect = None
# self.assertTrue(self.storage.exists(''))

def test_storage_exists(self):
obj = self.storage.bucket.Object.return_value
self.assertTrue(self.storage.exists("file.txt"))
Expand Down

0 comments on commit b92f50c

Please sign in to comment.