diff --git a/storages/backends/s3boto3.py b/storages/backends/s3boto3.py index 9caae4d0a..c5f721995 100644 --- a/storages/backends/s3boto3.py +++ b/storages/backends/s3boto3.py @@ -60,6 +60,8 @@ class S3Boto3StorageFile(File): buffer_size = setting('AWS_S3_FILE_BUFFER_SIZE', 5242880) def __init__(self, name, mode, storage, buffer_size=None): + if 'r' in mode and 'w' in mode: + raise ValueError("Can't combine 'r' and 'w' in mode.") self._storage = storage self.name = name[len(self._storage.location):].lstrip('/') self._mode = mode @@ -91,7 +93,7 @@ def _get_file(self): ) if 'r' in self._mode: self._is_dirty = False - self._file.write(self.obj.get()['Body'].read()) + self.obj.download_fileobj(self._file) self._file.seek(0) if self._storage.gzip and self.obj.content_encoding == 'gzip': self._file = GzipFile(mode=self._mode, fileobj=self._file, mtime=0.0) @@ -141,7 +143,9 @@ def _flush_write_buffer(self): self._write_counter += 1 self.file.seek(0) part = self._multipart.Part(self._write_counter) - part.upload(Body=self.file.read()) + part.upload(Body=self.file) + self.file.seek(0) + self.file.truncate() def close(self): if self._is_dirty: