I want to list files in a bucket from a Google Cloud Function in the same project.
Right now, my code for the Cloud Function is
main.py
from google.cloud import storage
def my_function(event, context):
storage_client = storage.Client()
blobs = storage_client.list_blobs("my_bucket")
for blob in blobs:
print(blob.name)
requirements.txt
google-cloud==0.34.0
The error I'm getting is
from google.cloud import storage ImportError: cannot import name 'storage' from 'google.cloud' (unknown location)
I was under the impression that google-cloud
would be pre-loaded on the machines running the Cloud Function but it seems like this is not the case.
I have tried:
- Reading the Google docs which is where the code comes from
- Reading other questions here, namely this (this one suggested that
google-cloud
is not pre-loaded which is why I added it to therequirements.txt
) and this (but I think there is a way to do this without settingGOOGLE_APPLICATION_CREDENTIALS
, as I'm calling from within the same project). Also read this one but the approved answer's code looks like mine, which isn't working.
How can I list the files on the bucket?
ModuleNotFoundError: No module named 'foo'
error you get when a module isn't installed. Googling a bit, that error seems to come up when you're trying to import a module from your own source tree and you don't quite have it right. I'm not sure how this applies to your project or Google Cloud in general. – CryptoFool