Google Cloud Storage - How to retrieve a file's owner via Python?

Viewed 292

I'm trying to access to a blob uploaded on a bucket of Google Cloud Storage via Python official client (google-cloud-storage). I'm managing into retrieving the owner of the file (the one who uploaded it), and I'm not finding something useful on internet.

I've tried using the client with something like:

client(project='project').get_bucket('bucket').get_blob('blob')

But the blob properties like "owner" are empty!

So I tried using a Cloud Function and accessing to event and context. In the Google documentation (https://cloud.google.com/storage/docs/json_api/v1/objects#resource) it is reported the structure of an event and it seems to have the owner propriety. But when I print or try to access it I obtain an error because it is not set.

Can someone help me? I just need to have the user email... thanks in advance!

EDIT:

It doesn't seem to be a permission error, because I obtain the correct results testing the API from the Google site: https://cloud.google.com/storage/docs/json_api/v1/objects/get?apix_params=%7B%22bucket%22%3A%22w3-dp-prod-technical-accounts%22%2C%22object%22%3A%22datagovernance.pw%22%2C%22projection%22%3A%22full%22%7D

1 Answers

By default, owner and ACL are not fetched by get_blob. You will have to explicitly fetch this info:

blob = client(project='project').get_bucket('bucket').get_blob('blob')
blob.reload(projection='full')

Note that if you use uniform bucket-level ACLs owner doesn't have any meaning and will be unset even with the above change.

EDIT: this is actually not the most efficient option because it makes an extra unnecessary call to GCS. The most efficient option is:

blob = Blob('bucket', 'blob')
blob.reload(projection='full', client=client)
Related