Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use create_anonymous_client() function in google cloud storage python library for access on public buckets?

I made a publicly listable bucket on google cloud storage. I can see all the keys if I try to list the bucket objects in the browser. I was trying to use the create_anonymous_client() function so that I can list the bucket keys in the python script. It is giving me an exception. I looked up everywhere and still can't find the proper way to use the function.

from google.cloud import storage

client = storage.Client.create_anonymous_client()
a = client.lookup_bucket('publically_listable_bucket')
a.list_blobs()

Exception I am getting:

ValueError: Anonymous credentials cannot be refreshed.

Additional Query: Can I list and download contents of public google cloud storage buckets using boto3, If yes, how to do it anonymously?

like image 325
Syed Shahrukh Ahmad Avatar asked Jun 28 '18 08:06

Syed Shahrukh Ahmad


People also ask

How do I access public Google storage bucket?

In the Google Cloud console, go to the Cloud Storage Buckets page. Click on the name of the bucket that contains the object you want to make public, and navigate to the object if it's in a subdirectory.

How do I access Google public data?

Access public datasets in the Google Cloud console The public dataset project is pinned to every project. To view the public datasets and tables in this project, see Displaying resources. If the project is not shown, search for "bigquery-public-data" and click "Broaden search to all projects" to find this project.


1 Answers

I was also struggling with thing and couldn't find an answer anywhere online. Turns out you can access the bucket with just the bucket() method.

I'm not sure why, but this method can take several seconds sometimes.

client = storage.Client.create_anonymous_client()

bucket = client.bucket('publically_listable_bucket')

blobs = list(bucket.list_blobs())
like image 146
msanchez-ayala Avatar answered Oct 17 '22 08:10

msanchez-ayala