Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleCloud: Set predefined_acl when using file write operations #756

Merged
merged 2 commits into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions storages/backends/gcloud.py
Expand Up @@ -74,8 +74,9 @@ def write(self, content):
def close(self):
if self._file is not None:
if self._is_dirty:
self.blob.upload_from_file(self.file, rewind=True,
content_type=self.mime_type)
self.blob.upload_from_file(
self.file, rewind=True, content_type=self.mime_type,
predefined_acl=self._storage.default_acl)
self._file.close()
self._file = None

Expand Down Expand Up @@ -173,14 +174,9 @@ def _save(self, name, content):
encoded_name = self._encode_name(name)
file = GoogleCloudFile(encoded_name, 'rw', self)
file.blob.cache_control = self.cache_control
if self.default_acl:
file.blob.upload_from_file(
content, rewind=True, size=content.size,
content_type=file.mime_type, predefined_acl=self.default_acl)
else:
file.blob.upload_from_file(
content, rewind=True, size=content.size,
content_type=file.mime_type)
file.blob.upload_from_file(
content, rewind=True, size=content.size,
content_type=file.mime_type, predefined_acl=self.default_acl)
return cleaned_name

def delete(self, name):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_gcloud.py
Expand Up @@ -83,6 +83,7 @@ def test_open_write(self, MockBlob):
# Simulate the file not existing before the write
self.storage._bucket = mock.MagicMock()
self.storage._bucket.get_blob.return_value = None
self.storage.default_acl = 'projectPrivate'

f = self.storage.open(self.filename, 'wb')
MockBlob.assert_called_with(self.filename, self.storage._bucket)
Expand All @@ -94,7 +95,8 @@ def test_open_write(self, MockBlob):

MockBlob().upload_from_file.assert_called_with(
tmpfile, rewind=True,
content_type=mimetypes.guess_type(self.filename)[0])
content_type=mimetypes.guess_type(self.filename)[0],
predefined_acl='projectPrivate')

def test_save(self):
data = 'This is some test content.'
Expand All @@ -104,7 +106,8 @@ def test_save(self):

self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(self.filename)[0])
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(self.filename)[0],
predefined_acl=None)

def test_save2(self):
data = 'This is some test ủⓝï℅ⅆℇ content.'
Expand All @@ -115,7 +118,8 @@ def test_save2(self):

self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0])
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0],
predefined_acl=None)

def test_save_with_default_acl(self):
data = 'This is some test ủⓝï℅ⅆℇ content.'
Expand Down