List buckets
Stay organized with collections
Save and categorize content based on your preferences.
List the Cloud Storage buckets in a project.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# List buckets\n\nList the Cloud Storage buckets in a project.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Listing buckets](/storage/docs/listing-buckets)\n\nCode sample\n-----------\n\n### C++\n\n\nFor more information, see the\n[Cloud Storage C++ API\nreference documentation](/cpp/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n namespace gcs = ::google::cloud::storage;\n using ::google::cloud::StatusOr;\n [](gcs::Client client) {\n int count = 0;\n gcs::ListBucketsReader bucket_list = client.ListBuckets();\n for (auto&& bucket_metadata : bucket_list) {\n if (!bucket_metadata) throw std::move(bucket_metadata).status();\n\n std::cout \u003c\u003c bucket_metadata-\u003ename() \u003c\u003c \"\\n\";\n ++count;\n }\n\n if (count == 0) {\n std::cout \u003c\u003c \"No buckets in default project\\n\";\n }\n }\n\n### C#\n\n\nFor more information, see the\n[Cloud Storage C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n\n using Google.Apis.Storage.v1.Data;\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;\n using System;\n using System.Collections.Generic;\n\n public class ListBucketsSample\n {\n public IEnumerable\u003cBucket\u003e ListBuckets(string projectId = \"your-project-id\")\n {\n var storage = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();\n var buckets = storage.ListBuckets(projectId);\n Console.WriteLine(\"Buckets:\");\n foreach (var bucket in buckets)\n {\n Console.WriteLine(bucket.Name);\n }\n return buckets;\n }\n }\n\n### Go\n\n\nFor more information, see the\n[Cloud Storage Go API\nreference documentation](https://pkg.go.dev/cloud.google.com/go/storage).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \t\"cloud.google.com/go/storage\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listBuckets lists buckets in the project.\n func listBuckets(w io.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, projectID string) ([]string, error) {\n \t// projectID := \"my-project-id\"\n \tctx := context.Background()\n \tclient, err := storage.NewClient(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"storage.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tctx, cancel := context.WithTimeout(ctx, time.Second*30)\n \tdefer cancel()\n\n \tvar buckets []string\n \tit := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Buckets(ctx, projectID)\n \tfor {\n \t\tbattrs, err := it.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn nil, err\n \t\t}\n \t\tbuckets = append(buckets, battrs.Name)\n \t\tfmt.Fprintf(w, \"Bucket: %v\\n\", battrs.Name)\n \t}\n \treturn buckets, nil\n }\n\n### Java\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import com.google.api.gax.paging.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.paging.Page.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;\n\n public class ListBuckets {\n public static void listBuckets(String projectId) {\n // The ID of your GCP project\n // String projectId = \"your-project-id\";\n\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();\n Page\u003cBucket\u003e buckets = storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_list_com_google_cloud_storage_Storage_BucketListOption____();\n\n for (https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket : buckets.iterateAll()) {\n System.out.println(bucket.getName());\n }\n }\n }\n\n### Node.js\n\n\nFor more information, see the\n[Cloud Storage Node.js API\nreference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n // Imports the Google Cloud client library\n const {Storage} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n async function listBuckets() {\n const [buckets] = await storage.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/storage_2.html();\n\n console.log('Buckets:');\n buckets.forEach(bucket =\u003e {\n console.log(bucket.name);\n });\n }\n\n listBuckets().catch(console.error);\n\n### PHP\n\n\nFor more information, see the\n[Cloud Storage PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n use Google\\Cloud\\Storage\\StorageClient;\n\n /**\n * List all Cloud Storage buckets for the current project.\n */\n function list_buckets(): void\n {\n $storage = new StorageClient();\n foreach ($storage-\u003ebuckets() as $bucket) {\n printf('Bucket: %s' . PHP_EOL, $bucket-\u003ename());\n }\n }\n\n### Python\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/storage/latest/\n\n\n def list_buckets():\n \"\"\"Lists all buckets.\"\"\"\n\n storage_client = https://cloud.google.com/python/docs/reference/storage/latest/.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()\n buckets = storage_client.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_list_buckets()\n\n for bucket in buckets:\n print(bucket.name)\n\n### Ruby\n\n\nFor more information, see the\n[Cloud Storage Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n def list_buckets\n require \"google/cloud/storage\"\n\n storage = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-storage-control-v2/latest/Google-Cloud-Storage.html.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html\n\n storage.buckets.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Policy-Bindings.html do |bucket|\n puts bucket.name\n end\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]