Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set metadata in Google Cloud Storage using gcloud-python

I am trying to upload a file to Google Cloud Storage using gcloud-python and set some custom metadata properties. To try this I have created a simple script.

import os

from gcloud import storage

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

blob = bucket.get_blob('kirby.png')
blob.metadata = blob.metadata or {}
blob.metadata['Color'] = 'Pink'
with open(os.path.expanduser('~/Pictures/kirby.png'), 'rb') as img_data:        
    blob.upload_from_file(img_data)

I am able to upload the file contents. After uploading the file I am able to manually set metadata from the developer console and retrieve it.

I can't figure out how to upload the metadata programmatically.

like image 318
Remco Haszing Avatar asked Oct 15 '15 09:10

Remco Haszing


1 Answers

We discussed on the issue tracker and it surfaced a "bug" in the implementation, or at the very least something which catches users off guard.

Accessing metadata via blob.metadata is read-only. Thus when mutating that result via

blob.metadata['Color'] = 'Pink'

it doesn't actually change the metadata stored on blob.

The current "fix" is to just build up

metadata = {'Color': 'Pink'}
blob.metadata = metadata
like image 137
bossylobster Avatar answered Oct 17 '22 10:10

bossylobster