Getting list of buckets in Google Cloud Storage using Python
Start your free 7-days trial now!
To follow along with this guide, please make sure to have:
created a service account and downloaded the private key (JSON file) for authentication (please check out my detailed guide)
installed the Python client library for Google Cloud Storage:pip install --upgrade google-cloud-storage
Getting list of buckets on Google Cloud Storage
To get a list of buckets currently on Google Cloud Storage (GCS) using Python, use the
from google.cloud import storage# Authenticate ourselves using the private key of the service accountpath_to_private_key = './gcs-project-354207-099ef6796af6.json'client = storage.Client.from_service_account_json(json_credentials_path=path_to_private_key)for bucket in client.list_buckets():print(bucket.name)example-bucket-skytownermy-demo-bucket-skytownertest-bucket-skytowner
Here, the results show that we currently have 3 buckets on GCS.