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 — Unable to set blob metadata · Issue #1185 · googleapis/google-cloud-python

Unable to set blob metadata #1185

New issue
Copy link
New issue
Copy link
Closed
Closed
Unable to set blob metadata#1185
Copy link
Assignees
lukesneeringer
Labels
api: storageIssues related to the Cloud Storage API.priority: p2Moderately-important priority. Fix may not be included in next release.

Description

@remcohaszing
remcohaszing
opened on Oct 15, 2015
Issue body actions

I am trying to set custom metadata on a blob. However, this does not work. I'm using the following script:

#!/usr/bin/env python3
import os

from gcloud import storage

client = storage.Client('super secret id')
bucket = client.get_bucket('super secret bucket')

blob = bucket.blob('kirby.png')
blob.metadata = dict(Color='Pink')
with open(os.path.expanduser('~/Pictures/kirby.png'), 'rb') as img_data:
    blob.upload_from_file(img_data)

When retrieving the blob or inspecing it in the developer console, the metadata is still unset.

Activity

Next
  1. tseaver commented on Oct 15, 2015

    @tseaver
    tseaver
    on Oct 15, 2015
    Contributor
    More actions

    Setting the metadata locally does not cause the instance to make the API call to patch it on the server, and we don't send metadata along with the upload_from_file() call. You need to add a call to blob.patch() to persist the changes to blob.metadata.

    Reacted by Jason Haury
  2. tseaver
    added
    api: storageIssues related to the Cloud Storage API.
    on Oct 15, 2015
  3. remcohaszing commented on Oct 15, 2015

    @remcohaszing
    remcohaszing
    on Oct 15, 2015
    Author
    More actions

    I modified the script:

    #!/usr/bin/env python3
    import os
    
    from gcloud import storage
    
    client = storage.Client('dc-app-api')
    bucket = client.get_bucket('appsemble-dev')
    
    blob = bucket.blob('kirby.png')
    with open(os.path.expanduser('~/Pictures/kirby.png'), 'rb') as img_data:
        blob.upload_from_file(img_data)
    blob.metadata = {'Color': 'Pink', 'Spam': 'Cheese'}
    blob.metadata['Foo'] = 'Bar'
    blob.patch()

    This sets the Color and Spam metadata fields, but not the Foo field. There is no warning about this whatsoever.

    Also there is no documentation on the Blob class on how to update metadata.

  4. dhermes commented on Oct 15, 2015

    @dhermes
    dhermes
    on Oct 15, 2015
    Contributor
    More actions

    Thanks for reporting. Looking into it now.

  5. dhermes commented on Oct 15, 2015

    @dhermes
    dhermes
    on Oct 15, 2015
    Contributor
    More actions

    Ahhh. This is because the Blob.metadata getter returns a copy.deepcopy of the dictionary stored, so doing

    blob.metadata['Foo'] = 'Bar'

    updates the copy, not the dictionary actually stored on the Blob. We should fix this.

    @tseaver WDYT?

  6. tseaver commented on Oct 15, 2015

    @tseaver
    tseaver
    on Oct 15, 2015
    Contributor
    More actions

    To allow mutation of the metadata in-place (as opposed to assigning to it wholesale), we would need a way to detect those mutations, in order to mark it as changed (for a subsequent call to blob.patch() to pick up).

  7. dhermes commented on Oct 15, 2015

    @dhermes
    dhermes
    on Oct 15, 2015
    Contributor
    More actions

    Right. Custom dict subclass?

  8. tseaver commented on Oct 15, 2015

    @tseaver
    tseaver
    on Oct 15, 2015
    Contributor
    More actions

    Right, overriding:

    • __delitem__
    • __setitem__
    • clear
    • update
    • setdefault
    • pop
    • popitem
  9. remcohaszing commented on Oct 15, 2015

    @remcohaszing
    remcohaszing
    on Oct 15, 2015
    Author
    More actions

    A custom dict class could work for the examples I provided. This would still not make a dict behave as expected tough. Since dicts are mutable, I would expect the following to work:

    metadata = {'Holy': 'Grail'}
    blob.metadata = metadata
    metadata['Ministry'] = 'Silly Walks'
    blob.patch()

    If the setter of blob.metadata would return this new class, this would no longer refer to the original dict that has been set.

  10. remcohaszing commented on Oct 15, 2015

    @remcohaszing
    remcohaszing
    on Oct 15, 2015
    Author
    More actions

    I think this subclass could work if no setter is defined. An empty metadata object could be set in the Blob constructor. This exposes the metadata as if it is a regular dict. This would break with the current behaviour, where setting metadata is allowed.

  11. dhermes commented on Oct 15, 2015

    @dhermes
    dhermes
    on Oct 15, 2015
    Contributor
    More actions

    The fact that the getter returns a copy is not set in stone. We did it to "protect" the integrity of the actual data own by the class, but didn't think through situations like this.

    We don't need a getter, but do need a setter to track changes on the object. Can't have a setter without a getter, so that's just how it's gotta be. (Also, all properties are stored on a non-public attribute called _properties, another reason for a getter.)

  12. remcohaszing commented on Oct 15, 2015

    @remcohaszing
    remcohaszing
    on Oct 15, 2015
    Author
    More actions

    I suppose it could look something like this:

    class Metadata(dict):
        def __init__(self, blob):
            self._blob = blob
    
        def __setitem__(self, key, value):
            super(self, Metadata).__setitem__(key, value)
            self._blob._patch_property('metadata', self)
    
        # XXX Repeat pattern for other modifier functions
    
    
    class Blob(_PropertyMixin):
        ...
    
        def __init__(self, ...)
            ...
            self._properties['metadata'] = Metadata(self)
    
        @property
        def metadata(self):
            return self._properties['metadata']

    This marks the metadata as modified if it gets edited and ensures the metadata can't be overridden using a setter.

    A side effect is that the metadata property is never None. I personally like that better anyway.

  13. saxenanurag commented on Dec 16, 2015

    @saxenanurag
    saxenanurag
    on Dec 16, 2015
    More actions
    blob.upload_from_file()
    blob.metadata = {'luke': 'skywalker'}
    blob.patch()

    worked for me. I was able to upload a file and apply the meta data after the file upload.

    Reacted by Manoj Nathwani and Tobias Flühmann
  14. tseaver commented on Dec 16, 2015

    @tseaver
    tseaver
    on Dec 16, 2015
    Contributor
    More actions

    @saxenanurag I'm in the middle of reviewing how we are using the ex-apitools code (now in gcloud.streaming), and can see how we should be able to send the patched metadata along with the upload request.

    Reacted by Tim E
  15. dhermes commented on Dec 17, 2015

    @dhermes
    dhermes
    on Dec 17, 2015
    Contributor
    More actions

    👍 What he said 😀

  16. 4 remaining items

  17. lukesneeringer
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Apr 19, 2017
  18. tseaver commented on Jun 5, 2017

    @tseaver
    tseaver
    on Jun 5, 2017
    Contributor
    More actions

    #3362 passes along all non-ACL blob properties with the (initial or only) upload request.

  19. tseaver
    closed this as completedon Jun 5, 2017
  20. gjkemnitz commented on Oct 20, 2018

    @gjkemnitz
    gjkemnitz
    on Oct 20, 2018
    More actions

    You can work around this bug by creating a completely new dictionary, assigning it to blob.metadata, and doing blob.patch():

    newdict={}
    for x in blob.metadata:
        newdict[x] = blob.metadata[x]
        newdict['mynewproperty'] = 'awesome'
    blob.metadata=newdict
    blob.patch()
    

    Your blob will now have the {'mynewproperty':'awesome'} metadata property in its stored metadata list on GCS.

    Reacted by Manoj Nathwani, Edgard Ochoa and Christopher Baklid Simione
  21. Prabakaran1410 commented on Nov 7, 2019

    @Prabakaran1410
    Prabakaran1410
    on Nov 7, 2019
    More actions

    With patch() the metadata of a file is getting updated properly. But this has two operations,

    1. First upload a file with no metadata
    2. Then updating the metadata using patch()

    Is there any option to upload the file along with its metadata? this should be a single step process

  22. tseaver commented on Nov 7, 2019

    @tseaver
    tseaver
    on Nov 7, 2019
    Contributor
    More actions

    @Prabakaran1410 You don't need to call patch on the blob after upload: since PR #3362, all properties assigned to the blob before upload are passed through.

    Reacted by Tim E
  23. paul121 commented on Jul 30, 2020

    @paul121
    paul121
    on Jul 30, 2020
    More actions

    I had some trouble getting this to work because the metadata was always None (even though I manually added values via the Storage UI). Later I realized I was using bucket.blob("name") rather than bucket.get_blob("name") so the metadata hadn't been loaded. Might help someone else.

    Reacted by JohnGale87 and Adriel Higor
  24. parthea
    added a commit that references this issue on Jul 6, 2023

    Fixed failed tests on Kokoro (Natural Language API) [(#1185)](GoogleC…

    e37000c
  25. parthea
    added 3 commits that reference this issue on Nov 22, 2025

    chore(main): release 2.33.0 (#1185)

    0b06500

    chore(deps): update all dependencies (#1185)

    b0cf643

    fix: Allow mtls sts endpoint for external account token urls. (#1185)

    61d5f7a
  26. parthea
    added 3 commits that reference this issue on Mar 2, 2026

    chore(main): release 2.33.0 (#1185)

    ccdf6ed

    chore: Update mypy version (#1185)

    e900c13

    chore(deps): update all dependencies (#1185)

    c4bd526
  27. 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

  • @lukesneeringer
    lukesneeringer

Labels

api: storageIssues related to the Cloud Storage API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

    Milestone

    No milestone

    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.