GeezerVison is showing github.com — storage: cannot set object properties · Issue #536 · googleapis/google-cloud-python
storage: cannot set object properties #536Labels api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DescriptionOr at least I haven't figured out how. This is how the documentation suggests it may work.
>>> from gcloud import storage
>>> bucket = storage.get_bucket('bucket', 'id')
>>> key = storage.key.Key(bucket=bucket, name='dir/file.webp',
... properties={'contentType': 'image/webp'})
>>> key.upload_from_filename('dir/file.webp')
Success?
>>> key.properties['contentType']
'image/webp'
>>> key.content_type
'image/webp'
It's a lie unfortunately. (Also verified with gsutil).
>>> webp = bucket.get_key('dir/file.webp')
>>> webp.properties['contentType']
u'application/unknown'
>>> webp.content_type
u'application/unknown'Activitychanged the title [-]cannot set object properties[/-] [+]storage: cannot set object properties[/+] on Jan 12, 2015added type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed on Jan 12, 2015pdknsk commented on Jan 12, 2015 Just an additional minor remark. gsutil prefers application/octet-stream to application/unknown. dhermes commented on Jan 12, 2015 @pdknsk Thanks for the feedback, we'll look into it. (Also thanks for hunting down a more sensible default.) tseaver commented on Jan 12, 2015 Note that upload_from_filename overwrites any previously-set contentType (via upload_from_file). So, the fix would be to call self._reload_properties() at the end of upload_from_file. pdknsk commented on Jan 15, 2015 I applied the fix and the result is this.
>>> key.content_type
'image/webp'
>>> key.upload_from_filename('dir/file.webp')
>>> key.content_type
u'application/unknown'
Which is correct in a way, because that's how the object is stored online, even if incorrectly so.
I tried Cache-Control (or cacheControl rather) and it doesn't work either. pdknsk commented on Jan 28, 2015 I've figured out that in order to send metadata with the request it has to be set on the request body before upload.ConfigureRequest is called. This works for Content-Type and Cache-Control in this case.
--- a/gcloud/storage/blob.py
+++ b/gcloud/storage/blob.py
@@ -296,7 +296,7 @@ class Blob(_PropertyMixin):
}
upload = transfer.Upload(file_obj,
- content_type or 'application/unknown',
+ content_type or self.properties.get('contentType', 'application/unknown'),
total_bytes, auto_transfer=False,
chunksize=self.CHUNK_SIZE)
@@ -311,6 +311,11 @@ class Blob(_PropertyMixin):
# Use apitools 'Upload' facility.
request = http_wrapper.Request(upload_url, 'POST', headers)
+ if self.properties:
+ import json
+ headers['content-type'] = 'application/json'
+ request.body = json.dumps(self.properties)
+
upload.ConfigureRequest(upload_config, request, url_builder)
query_params = url_builder.query_params
request.url = conn.build_api_url(path=self.bucket.path + '/o',dhermes commented on Feb 13, 2015 So the core issue here is that Blob.upload_from_filename uses mimetypes rather than the given content_type. I tested with a real webp file and mimetypes doesn't know what to think.
You could "get around this" by using upload_from_file directly. dhermes commented on Feb 13, 2015 @tseaver I'm going to send a fix and we should discuss the "right approach" in the PR.
MetadataMetadataapi: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DevelopmentNo branches or pull requestsIssue actions
|