question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to set blob metadata

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
pdknskcommented, Mar 24, 2016

I think this is a duplicate of #754. The use of metadata is a bit ambiguous as metadata refers to both the metadata of an object (such as Cache-Control) and the metadata property, meant for custom metadata.

Anyway, to the issue. Any update? The problem with patch is that the object is online briefly with wrong metadata, or less briefly in case of a connection failure between two requests. And, it increases metageneration.

>>> text = bucket.blob('file.txt')
>>> text.cache_control = 'no-cache'
>>> text.upload_from_string('text')
>>> text.reload()
>>> text.cache_control
>>> text.metageneration
1
>>> text.cache_control = 'no-cache'
>>> text.patch()
>>> text.reload()
>>> text.cache_control
u'no-cache'
>>> text.metageneration
2
3reactions
gjkemnitzcommented, Oct 20, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AzureBlob: Unable to set blob metadata (x-ms-meta-* headers ...
It is currently impossible to set blob metadata when calling commit_block_list(). The _commit_block_list_options() method does not pass the ...
Read more >
c# - Can't set Azure Block Blob Metadata on upload time
I can't setup metadata on the upload time. If I call await blockBlob.SetMetadataAsync(); before UploadFromStreamAsync() , I get error: Microsoft ...
Read more >
Set Blob Metadata (REST API) - Azure Storage | Microsoft Learn
The Set Blob Metadata operation sets user-defined metadata for the specified blob as one or more name-value pairs.
Read more >
Metadata Load for Microsoft Azure Blob Storage fails with ...
Metadata Load for Microsoft Azure Blob Storage fails and the following is from the stdout.log. ... at java.lang.Thread.run(Thread.java:748) Caused by: javax.net.
Read more >
Setting and Retrieving Azure Blob Storage Properties and ...
Explore how to manage blob properties and metadata using the Azure Portal and ... You'll notice that these properties can't be modified, which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found