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

Allow passing predefined_acl via gcs blob_properties #727

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion smart_open/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import google.cloud.exceptions
import google.cloud.storage
import google.auth.transport.requests
_PREDEFINED_ACL_SUPPORTED = google.cloud.storage.__version__.split('.') >= ['2', '6']
except ImportError:
MISSING_DEPS = True

Expand Down Expand Up @@ -420,14 +421,20 @@ def __init__(

self._session = google.auth.transport.requests.AuthorizedSession(client._credentials)

upload_kwargs = {}

if blob_properties:
if 'predefined_acl' in blob_properties:
upload_kwargs['predefined_acl'] = blob_properties.pop('predefined_acl')
for k, v in blob_properties.items():
setattr(self._blob, k, v)

if 'predefined_acl' in upload_kwargs and not _PREDEFINED_ACL_SUPPORTED:
raise NotImplementedError('GCS "predefined_acl" requires google-cloud-storage>=2.6.0')
#
# https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#start-resumable
#
self._resumable_upload_url = self._blob.create_resumable_upload_session()
self._resumable_upload_url = self._blob.create_resumable_upload_session(**upload_kwargs)

#
# This member is part of the io.BufferedIOBase interface.
Expand Down
7 changes: 5 additions & 2 deletions smart_open/tests/test_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ def __init__(self, name, bucket):

self._create_if_not_exists()

def create_resumable_upload_session(self):
def create_resumable_upload_session(self, predefined_acl=None):
resumeable_upload_url = RESUMABLE_SESSION_URI_TEMPLATE % dict(
bucket=self._bucket.name,
upload_id=str(uuid.uuid4()),
)
upload = FakeBlobUpload(resumeable_upload_url, self)
self.acl = predefined_acl
self._bucket.register_upload(upload)
return resumeable_upload_url

Expand Down Expand Up @@ -807,12 +808,14 @@ def test_write_05(self):
smart_open_write = smart_open.gcs.Writer(BUCKET_NAME, WRITE_BLOB_NAME,
blob_properties={
"content_type": "random/x-test",
"content_encoding": "coded"
"content_encoding": "coded",
"predefined_acl": "publicRead"
}
)
with smart_open_write as fout: # noqa
assert fout._blob.content_type == "random/x-test"
assert fout._blob.content_encoding == "coded"
assert fout._blob.acl == "publicRead"

def test_gzip(self):
expected = u'а не спеть ли мне песню... о любви'.encode('utf-8')
Expand Down