I try to add the rowcount to the metadata of the Google Cloud Storage file from inside the Python Google Cloud Function.
I follow the official Cloud Storage Docs example of "Edit object metadata" by just trying to change an attribute the same way as in the code:
from google.cloud import storage
def set_blob_metadata(bucket_name, blob_name):
"""Set a blob's metadata."""
# bucket_name = 'your-bucket-name'
# blob_name = 'your-object-name'
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.get_blob(blob_name)
metadata = {'color': 'Red', 'name': 'Test'}
blob.metadata = metadata
blob.patch()
print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))
My code, just setting one attribute:
from os import environ
from google.cloud import storage
...
storage_client = storage.Client()
bucket = storage_client.bucket(environ["BUCKET_NAME"])
# Retrieve a blob, and its metadata, from Google Cloud Storage.
# Note that `get_blob` differs from `Bucket.blob`, which does not
# make an HTTP request.
blob = bucket.get_blob(environ["FILE_NAME_TEST"])
print("Size: {} bytes".format(blob.size))
print("Custom Time: {}".format(blob.custom_time))
blob.size = 99
But this throws an error in the logs:
blob.size = 99 AttributeError: can't set attribute"
How to write to an attribute using the Python google_cloud_storage package?