Skip to content Skip to sidebar Skip to footer

How To Access File Metadata, For Files In Google Cloud Storage, From A Python Google Cloud Function

I'm trying to access the custom metadata on a file in Google cloud storage from within a Cloud Function, but it always returns 'None'. The file definitely has custom metadata on i

Solution 1:

blob.metadata only returns Storage object's custom metadata (a dict). None means that there is no custom metadata. See the docs of metadata :

Retrieve arbitrary/application specific metadata for the object.

The documentation of Object resource (API) specify that metadata is :

User-provided metadata, in key/value pairs.

Note that custom metadata is different from fixed-key metadata, that you can also edit with Edit metadata button in Google Cloud Console. Fixed-key metadata contains :

  • Content-Type
  • Content-Encoding
  • Content-Disposition
  • Content-Language
  • Cache-Control

This particular kind of metadata can be accessed via blob.content_type, blob.content_encoding, ... (check a complete example).

To add custom metadata, just click Add item button on the same window (Edit metadata) or use gsutil (see Editing object metadata docs) :

gsutil setmeta -h "x-goog-meta-examplekey:examplevalue" gs://<your-bucket>

Solution 2:

In fact blob.metadata will not show the user metadata, you have to add blob.patch() and metadata will appear in blob.metadata variable

from google.cloud import storage
client = storage.Client()
bucket = client.bucket(<my bucket name>)
blob = bucket.get_blob(<my filename>)
blob.patch()
metadata = blob.metadata

To save user metadata, you can set blob.metadata and then call blob.patch to add or modify metadata, or blob.update to erase user metadata for this blob

Solution 3:

Use blob.get_metadata, NOT blob.metadata

From the docs, blob.metadata does not make an HTTP request.

Solution 4:

I ran into the same problem. But in my case, I have a trigger to run a cloud function whenever an object is created in a bucket. I needed to get the metadata as well using the code that you used, but it returns None as well.

I then changed the trigger to google.storage.object.metadataUpdate, so my Cloud Function would trigger whenever I set or update the metadata of an object. By doing that, I could get the metadata using the same code.

Post a Comment for "How To Access File Metadata, For Files In Google Cloud Storage, From A Python Google Cloud Function"