Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google.cloud import storage: cannot import storage

Tags:

I tried to run the code bellow by following the google tutorials i found here: https://cloud.google.com/docs/authentication/production

def implicit():     from google.cloud import storage      # If you don't specify credentials when constructing the client, the     # client library will look for credentials in the environment.     project = 'my_project_name'     storage_client = storage.Client(project=project)      # Make an authenticated API request     buckets = list(storage_client.list_buckets())     print(buckets)  implicit() 

But it keeps giving me the following error:

Traceback (most recent call last):   File "[PATH]/scratch_5.py", line 13, in <module>     implicit()   File "[PATH]/scratch_5.py", line 2, in implicit     from google.cloud import storage ImportError: cannot import name storage 

Could someone help me with this?

like image 865
Mariane Reis Avatar asked Jun 13 '18 15:06

Mariane Reis


People also ask

How do I upload data to Google Cloud Storage?

In the Google Cloud console, go to the Cloud Storage Buckets page. In the list of buckets, click on the name of the bucket that you want to upload an object to. In the Objects tab for the bucket, either: Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console.


2 Answers

I see you are trying to use the Google Cloud Storage client libraries.

In order to use it, you should first make sure that it is installed in your machine:

pip install --upgrade google-cloud-storage 

And then, you should probably set up authentication (if you are using Application Default Credentials, from the documentation you mentioned), by setting up the GOOGLE_APPLICATION_CREDENTIALS environment variable in the machine where you are running the code, like below. If you are using Windows, follow the steps presented in the documentation, instead.

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file.json" 

Alternatively, you can try using explicit credentials. The only difference between the one you shared (using implicit credentials obtained from the environment) and one using explicit credentials, is that when you declare the GCS client, you should do something like:

storage_client = storage.Client.from_service_account_json('/path/to/SA_key.json') 

Once all this is ready, you should have no issues with running the sample code you provided. In order to keep learning about GCS and its client libraries, feel free to search on the documentation I linked and have a look at the library reference page.

like image 158
dsesto Avatar answered Sep 19 '22 08:09

dsesto


Also, make sure your main.py file and the requirements.txt are in the same directory and the same directory as the function being deployed.

Just FYI, because I had to do this even after specifying my environment variables.

like image 31
Gidi9 Avatar answered Sep 17 '22 08:09

Gidi9