GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — storage: cannot set object properties · Issue #536 · googleapis/google-cloud-python

storage: cannot set object properties #536

New issue
Copy link
New issue
Copy link
Closed
#629
Closed
storage: cannot set object properties#536
#629
Copy link
Assignees
dhermes
Labels
api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
Milestone
Storage Stable

Description

@pdknsk
pdknsk
opened on Jan 12, 2015
Issue body actions

Or 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'

Activity

Next
  1. pdknsk
    changed the title [-]cannot set object properties[/-] [+]storage: cannot set object properties[/+] on Jan 12, 2015
  2. dhermes
    added
    api: datastoreIssues related to the Datastore API.
    api: storageIssues related to the Cloud Storage API.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    and removed
    api: datastoreIssues related to the Datastore API.
    on Jan 12, 2015
  3. pdknsk commented on Jan 12, 2015

    @pdknsk
    pdknsk
    on Jan 12, 2015
    Author
    More actions

    Just an additional minor remark. gsutil prefers application/octet-stream to application/unknown.

  4. dhermes commented on Jan 12, 2015

    @dhermes
    dhermes
    on Jan 12, 2015
    Contributor
    More actions

    @pdknsk Thanks for the feedback, we'll look into it. (Also thanks for hunting down a more sensible default.)

  5. tseaver commented on Jan 12, 2015

    @tseaver
    tseaver
    on Jan 12, 2015
    Contributor
    More actions

    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.

  6. pdknsk commented on Jan 15, 2015

    @pdknsk
    pdknsk
    on Jan 15, 2015
    Author
    More actions

    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.

  7. pdknsk commented on Jan 28, 2015

    @pdknsk
    pdknsk
    on Jan 28, 2015
    Author
    More actions

    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',
  8. jgeewax
    modified the milestone: Storage Stable on Jan 30, 2015
  9. dhermes commented on Feb 13, 2015

    @dhermes
    dhermes
    on Feb 13, 2015
    Contributor
    More actions

    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.

  10. dhermes commented on Feb 13, 2015

    @dhermes
    dhermes
    on Feb 13, 2015
    Contributor
    More actions

    @tseaver I'm going to send a fix and we should discuss the "right approach" in the PR.

  11. dhermes
    self-assigned this
    on Feb 13, 2015
  12. 15 remaining items

  13. parthea
    added 3 commits that reference this issue on Oct 21, 2023

    chore(main): release 2.14.2 (#536)

    545e82d

    chore(main): release 3.4.3 (#536)

    2c73050

    build: [autoapprove] bump cryptography from 41.0.2 to 41.0.3 (#536)

    60ac91b
  14. parthea
    added a commit that references this issue on Aug 21, 2025

    chore(main): release 2.17.0 (#536)

    a2cc256
  15. parthea
    added a commit that references this issue on Sep 16, 2025

    chore(main): release 2.17.0 (#536)

    488ed7f
  16. parthea
    added 5 commits that reference this issue on Nov 22, 2025

    chore(main): release 2.7.1 (#536)

    f839ab7

    fix(deps): allow protobuf 6.x (#536)

    cfec2bb

    chore: [autoapprove] Update `black` and `isort` to latest versions (#536

    b354b62

    fix: ordering in limit_to_last (#692)

    c9c7251

    feat: memcached integration (#536)

    c43ea90
  17. parthea
    added 4 commits that reference this issue on Mar 6, 2026

    chore(deps): update dependency grpcio to v1.36.0 (#536)

    7e86d3f

    test: run system tests against emulator (#536)

    416121f

    chore(deps): update dependency six to v1.17.0 (#536)

    0c427af

    chore: avoid `.nox` directories when building docs (#536)

    b37fb69
  18. parthea
    added a commit that references this issue on Sep 8, 2026

    docs: migrate code from googleapis/python-bigquery (#18303)

    23f9a74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dhermes
    dhermes

Labels

api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    • Storage Stable

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.