Listing Buckets

This page shows you how to list the Cloud Storage buckets in a project, which are ordered in the list lexicographically by name. For an overview of buckets, read the Key Terms.

To list the buckets in a project:

Console

Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser

Buckets that are part of the currently selected project appear in the browser list.

gsutil

Use the gsutil ls command:

gsutil ls

The response looks like the following example:

gs://[BUCKET_NAME1]/
gs://[BUCKET_NAME2]/
gs://[BUCKET_NAME3]/
...

Code samples

C#

For more information, see the Cloud Storage C# API reference documentation .

private void ListBuckets()
{
    var storage = StorageClient.Create();
    foreach (var bucket in storage.ListBuckets(s_projectId))
    {
        Console.WriteLine(bucket.Name);
    }
}

Go

For more information, see the Cloud Storage Go API reference documentation .

var buckets []string
it := client.Buckets(ctx, projectID)
for {
	battrs, err := it.Next()
	if err == iterator.Done {
		break
	}
	if err != nil {
		return nil, err
	}
	buckets = append(buckets, battrs.Name)
}

Java

For more information, see the Cloud Storage Java API reference documentation .

// Include a prefix of bucket-name to reduce search space.
// For more information read https://cloud.google.com/storage/docs/json_api/v1/buckets/list
Page<Bucket> buckets =
    storage.list(BucketListOption.pageSize(100), BucketListOption.prefix(prefix));
for (Bucket bucket : buckets.iterateAll()) {
  // do something with the bucket
}

Node.js

For more information, see the Cloud Storage Node.js API reference documentation .

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

// Lists all buckets in the current project
storage
  .getBuckets()
  .then(results => {
    const buckets = results[0];

    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

For more information, see the Cloud Storage PHP API reference documentation .

use Google\Cloud\Storage\StorageClient;

/**
 * List all Cloud Storage buckets for the current project.
 *
 * @return void
 */
function list_buckets()
{
    $storage = new StorageClient();
    foreach ($storage->buckets() as $bucket) {
        printf('Bucket: %s' . PHP_EOL, $bucket->name());
    }
}

Ruby

For more information, see the Cloud Storage Ruby API reference documentation .

# project_id = "Your Google Cloud project ID"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project: project_id

storage.buckets.each do |bucket|
  puts bucket.name
end

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the JSON API with a LIST Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://www.googleapis.com/storage/v1/b?project=[PROJECT_NAME]"

    You can use a prefix=[PREFIX] query string parameter to limit results to buckets that have the specified prefix.

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a GET Service request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "x-goog-project-id: [PROJECT_NAME]" \
        "https://storage.googleapis.com"

    You can use a prefix=[PREFIX] query string parameter to limit results to buckets that have the specified prefix.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Storage
Need help? Visit our support page.