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

support for predefined ACL to be set directly on the upload request #1660

Closed
pdknsk opened this issue Mar 25, 2016 · 5 comments
Closed

support for predefined ACL to be set directly on the upload request #1660

pdknsk opened this issue Mar 25, 2016 · 5 comments
Assignees
Labels
api: storage Issues related to the Cloud Storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@pdknsk
Copy link

pdknsk commented Mar 25, 2016

This is a follow-up to #651.

This request may be a bit too early, as it will only really matter when #1185 is fixed. Right now with blob.acl.save_predefined the object ACL can be set to a predefined value separately, like the metadata can be with blob.patch. The idea is to set everything with the upload request, to not have separate request.

@dhermes dhermes added the api: storage Issues related to the Cloud Storage API. label Mar 25, 2016
@pdknsk
Copy link
Author

pdknsk commented Apr 7, 2017

I've made a simple patch for common use cases, on top of another patch. It does not check the name of the predefined ACL, nor does it translate from XML to JSON names.

--- a/blob.py
+++ b/blob.py
@@ -407,7 +407,8 @@
 
     def _create_upload(
             self, client, file_obj=None, size=None, content_type=None,
-            chunk_size=None, strategy=None, extra_headers=None):
+            chunk_size=None, strategy=None, extra_headers=None,
+            predefined_acl=None):
         """Helper for upload methods.
 
         Creates a :class:`google.cloud.core.streaming.Upload` object to handle
@@ -482,6 +483,7 @@
             upload.strategy = RESUMABLE_UPLOAD
 
         url_builder = _UrlBuilder(
+            predefined_acl=predefined_acl,
             bucket_name=self.bucket.name,
             object_name=self.name)
         upload_config = _UploadConfig()
@@ -497,6 +499,9 @@
 
         if self._properties:
             headers['content-type'] = 'application/json'
+            # 409 Cannot provide both a predefinedAcl and access controls
+            if predefined_acl and 'acl' in self._properties:
+                del self._properties['acl']
             request.body = json.dumps(self._properties)
 
         upload.configure_request(upload_config, request, url_builder)
@@ -525,7 +530,8 @@
                                  error_info=request.url)
 
     def upload_from_file(self, file_obj, rewind=False, size=None,
-                         content_type=None, num_retries=6, client=None):
+                         content_type=None, num_retries=6, client=None,
+                         predefined_acl=None):
         """Upload the contents of this blob from a file-like object.
 
         The content type of the upload will either be
@@ -621,7 +627,7 @@
         upload, request, _ = self._create_upload(
             client, file_obj=file_obj, size=total_bytes,
             content_type=content_type, chunk_size=chunk_size,
-            strategy=strategy)
+            strategy=strategy, predefined_acl=predefined_acl)
 
         if upload.strategy == RESUMABLE_UPLOAD:
             http_response = upload.stream_file(use_chunks=True)
@@ -637,7 +643,8 @@
             response_content = response_content.decode('utf-8')
         self._set_properties(json.loads(response_content))
 
-    def upload_from_filename(self, filename, content_type=None, client=None):
+    def upload_from_filename(self, filename, content_type=None, client=None,
+                             predefined_acl=None):
         """Upload this blob's contents from the content of a named file.
 
         The content type of the upload will either be
@@ -675,7 +682,8 @@
             self.upload_from_file(
                 file_obj, content_type=content_type, client=client)
 
-    def upload_from_string(self, data, content_type='text/plain', client=None):
+    def upload_from_string(self, data, content_type='text/plain', client=None,
+                           predefined_acl=None):
         """Upload contents of this blob from the provided string.
 
         .. note::
@@ -708,7 +716,8 @@
         string_buffer.write(data)
         self.upload_from_file(
             file_obj=string_buffer, rewind=True, size=len(data),
-            content_type=content_type, client=client)
+            content_type=content_type, client=client,
+            predefined_acl=predefined_acl)
 
     def upload_from_bytes(self, data, content_type=None, client=None):
 
@@ -1211,8 +1220,10 @@
 
 class _UrlBuilder(object):
     """Faux builder FBO apitools' 'configure_request'"""
-    def __init__(self, bucket_name, object_name):
+    def __init__(self, bucket_name, object_name, predefined_acl):
         self.query_params = {'name': object_name}
+        if predefined_acl:
+            self.query_params['predefinedAcl'] = predefined_acl
         self._bucket_name = bucket_name
         self._relative_path = ''
 

@pdknsk
Copy link
Author

pdknsk commented Apr 7, 2017

--- a/blob.py
+++ b/blob.py
@@ -680,7 +680,8 @@
 
         with open(filename, 'rb') as file_obj:
             self.upload_from_file(
-                file_obj, content_type=content_type, client=client)
+                file_obj, content_type=content_type, client=client,
+                predefined_acl=predefined_acl)
 
     def upload_from_string(self, data, content_type='text/plain', client=None,
                            predefined_acl=None):

@lukesneeringer lukesneeringer added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Apr 19, 2017
@tseaver tseaver added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jan 8, 2018
@chemelnucfin chemelnucfin self-assigned this Jan 15, 2018
@chemelnucfin
Copy link
Contributor

Hello, may I ask if you can make a pull request with this patch?

Or may I submit a PR on your behalf using the code above?

@pdknsk
Copy link
Author

pdknsk commented Jan 16, 2018

Sure, though I think you should perhaps add XML names to JSON names translation.

@chemelnucfin
Copy link
Contributor

chemelnucfin commented Jan 22, 2018

Hello, feature requests will now be tracked in the project Feature Requests. I will close this issue now, please feel free to continue to address any issues/concerns here.

parthea pushed a commit that referenced this issue Jul 6, 2023
…oogleCloudPlatform/python-docs-samples#1660)

* Generated sample: language_sentiment_text

FYI generated from the following YAML GAPIC config:

  sample_value_sets:
    - id: analyze_sentiment
      title: "Analyzing Sentiment"
      description: "Proof of concept for analyzing sentiment"
      parameters:
        defaults:
        - document.type=PLAIN_TEXT
        - document.content="Your text to analyze, e.g. Hello, world!"
        attributes:
        - parameter: document.content
          sample_argument: true
      on_success:
      - define: sentiment=$resp.document_sentiment
      - print:
        - "Score: %s"
        - sentiment.score
      - print:
        - "Magnitude: %s"
        - sentiment.magnitude
    samples:
      standalone:
      - calling_forms: ".*"
        value_sets: analyze_sentiment
        region_tag: language_sentiment_text

* Add requirements.txt (not currently generated)

* Add test for language_sentiment_text (not currently generated)

* Move language_python_migration_document_text

Move language_python_migration_document_text so it uses a different snippet in preparation for deprecation of existing language_sentiment_text sample

* Rename generated snippets so filename == region tag

* Fix test for generated code sample (file rename to match region tag)

* Update Copyright year to 2018 in new hand-written file

* Fix lint errors of #language_sentiment_text test

* Regenerate #language_sentiment_text to fix lint errors (updated Python sample template)

* Binary string support in samples!

From PR googleapis/gapic-generator#2272
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the Cloud Storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

5 participants