0

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?

1 Answer 1

0

It may be obvious from the example, but I did not get it. I thought that "metadata" would be all of your metadata and that you could overwrite it as you like, so that {'color': 'Red', 'name': 'Test'} would actually replace the whole metadata at hand.

I even created

    blob.metadata = {
        **metadata,
        **{
            "rowcount": 99,
            "columncount": 55
        },
    }

to join the filled metadata with the additional attributes. But this is of course not needed. And you also cannot overwrite any default attributes of the metadata either.

If you want to save something in the metadata, you must add them as dictionary to the "metadata" attribute.

In my case, when I want to store the rowcount, I just need:

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 = {'rowcount': 555}
    blob.metadata = metadata
    blob.patch()

    print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))

Wrap up

I thought at first that the metadata attribute is just all metadata in one attribute as a dictionary, but instead, it is None by default and you can fill it with a dictionary of your choice to add new attributes. Only this "metadata" attribute can be filled at all, any other attribute throws AttributeError: can't set attribute".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.