Using Bucket Labels

This page shows you how to add, view, edit, and remove the labels set on a bucket in Cloud Storage. For an overview of buckets and bucket labels, see the Key Terms.

Add a bucket label

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser
  2. In the bucket list, find the bucket you want to apply a label to, and click its more options button (three vertical dots).

  3. Click Edit labels.

  4. In the side panel that appears, click the + Add label button.

  5. Specify a key and value for your label.

  6. Click Save.

In the Edit labels side panel, you can also edit the Value of existing labels.

gsutil

Use the -l flag in a label ch command. For example:

gsutil label ch -l [KEY_1]:[VALUE_1] gs://[BUCKET_NAME]/

Where [KEY_1] is the key name for your label, [VALUE_1] is the value for your label and [BUCKET_NAME] is the name of the bucket that the label applies to.

  • Use multiple -l flags to add or edit multiple key:value pairs in a single command.

  • Use the label set command to replace all existing labels with new ones.

Code samples

C++

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

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name, std::string label_key,
   std::string label_value) {
  gcs::BucketMetadata updated_metadata = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetLabel(label_key, label_value));
  std::cout << "Successfully set label " << label_key << " to " << label_value
            << " on bucket  " << bucket_name << ".";
  std::cout << " The bucket labels are now:";
  for (auto const& kv : updated_metadata.labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << std::endl;
}

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Adds or updates a bucket label.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $labelName the name of the label to add.
 * @param string $labelValue the value of the label to add.
 */
function add_bucket_label($bucketName, $labelName, $labelValue)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $newLabels = [$labelName => $labelValue];
    $bucket->update(['labels' => $newLabels]);
    printf('Added label %s (%s) to %s' . PHP_EOL, $labelName, $labelValue, $bucketName);
}

Python

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

def add_bucket_label(bucket_name):
    """Add a label to a bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    labels = bucket.labels
    labels['example'] = 'label'
    bucket.labels = labels
    bucket.patch()

    print('Updated labels on {}.'.format(bucket.name))
    pprint.pprint(bucket.labels)

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"
# label_key   = "Cloud Storage bucket Label Key"
# label_value = "Cloud Storage bucket Label Value"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name

bucket.update do |bucket|
  bucket.labels[label_key] = label_value
end

puts "Added label #{label_key} with value #{label_value} to #{bucket_name}"

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. Create a .json file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
  3. {
      "labels": {
        "[KEY_1]": "[VALUE_1]"
      }
    }
  4. Use cURL to call the JSON API with a PATCH Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Type: application/json" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=labels"

You can add or edit multiple key:value pairs in a request.

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .xml file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    <Tagging>
      <TagSet>
        <Tag>
          <Key>[KEY_1]</Key>
          <Value>[VALUE_1]</Value>
        </Tag>
      </TagSet>
    </Tagging>

    Note that you can add multiple <Tag> elements in a request.

  3. Use cURL to call the XML API, with a PUT Bucket request and tagging query string parameter, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

View bucket labels

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser
  2. In the bucket list, find the bucket you want to verify, and check its Label values.

    Verify Multi-Regional Storage bucket class.

gsutil

Use following gsutil ls command, replacing [VALUES_IN_BRACKETS] with the appropriate values:

gsutil ls -L -b gs://[BUCKET_NAME]/

The response looks like the following example:

gs://[BUCKET_NAME]/ :
  Storage class:         MULTI_REGIONAL
  ...
  ...
  Labels:
    {
      "your_label_key": "your_label_value"
    }
  ...

Code samples

C++

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

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name) {
  gcs::BucketMetadata metadata =
      client.GetBucketMetadata(bucket_name, gcs::Fields("labels"));
  if (metadata.labels().empty()) {
    std::cout << "The bucket " << bucket_name << " has no labels set."
              << std::endl;
    return;
  }
  std::cout << "The labels for bucket " << bucket_name << " are:";
  for (auto const& kv : metadata.labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << std::endl;
}

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Prints a list of a bucket's lables.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 */
function get_bucket_labels($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $info = $bucket->info();
    if (isset($info['labels'])) {
        foreach ($info['labels'] as $labelKey => $labelValue) {
            printf('%s: %s' . PHP_EOL, $labelKey, $labelValue);
        }
    }
}

Python

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

def get_bucket_labels(bucket_name):
    """Prints out a bucket's labels."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    labels = bucket.labels
    pprint.pprint(labels)

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name

puts "Labels for #{bucket_name}"
bucket.labels.each do |key, value|
  puts "#{key} = #{value}"
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 GET Bucket request that includes the desired fields, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=labels"

The response looks like the following example:

{
  "labels" : {
     ([YOUR_LABEL_KEY]): [YOUR_LABEL_VALUE]
   },
}

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 Bucket request that includes the tagging query parameter, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

The response looks like the following example:

<Tagging>
  <TagSet>
    <Tag>
      <Key>[YOUR_LABEL_KEY]</Key>
      <Value>[YOUR_LABEL_VALUE]</Value>
    </Tag>
  </TagSet>
</Tagging>

Remove a bucket label

Console

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    Open the Cloud Storage browser
  2. In the bucket list, find the bucket whose label you want to remove, and click the bucket's more options button (three vertical dots).

  3. Click Edit labels.

  4. Click the X associated with the label you want to remove.

  5. Click Save.

gsutil

Use the -d flag in a label ch command. For example:

gsutil label ch -d [KEY_1] gs://[BUCKET_NAME]/

Where [KEY_1] is the key name for your label and [BUCKET_NAME] is the name of the bucket that the label applies to.

  • Use the label set command to replace all existing labels with new ones.

Code samples

C++

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

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name, std::string label_key) {
  gcs::BucketMetadata updated_metadata = client.PatchBucket(
      bucket_name, gcs::BucketMetadataPatchBuilder().ResetLabel(label_key));
  std::cout << "Successfully reset label " << label_key << " on bucket  "
            << bucket_name << ".";
  if (updated_metadata.labels().empty()) {
    std::cout << " The bucket now has no labels." << std::endl;
    return;
  }
  std::cout << " The bucket labels are now:";
  for (auto const& kv : updated_metadata.labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << std::endl;
}

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Removes a label from a bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $labelName the name of the label to remove.
 */
function remove_bucket_label($bucketName, $labelName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $labels = [$labelName => null];
    $bucket->update(['labels' => $labels]);
    printf('Removed label %s from %s' . PHP_EOL, $labelName, $bucketName);
}

Python

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

def remove_bucket_label(bucket_name):
    """Remove a label from a bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    labels = bucket.labels

    if 'example' in labels:
        del labels['example']

    bucket.labels = labels
    bucket.patch()

    print('Updated labels on {}.'.format(bucket.name))
    pprint.pprint(bucket.labels)

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"
# label_key   = "Cloud Storage bucket Label Key"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name

bucket.update do |bucket|
  bucket.labels[label_key] = nil
end

puts "Deleted label #{label_key} from #{bucket_name}"

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. Create a .json file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
  3. {
      "labels": {
        "[KEY_1]": null
      }
    }
  4. Use cURL to call the JSON API with a PATCH Bucket request, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        -H "Content-Type: application/json" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=labels"

You can remove multiple key:value pairs in a request.

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .xml file that contains the following information, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    <Tagging>
      <TagSet>
        <Tag>
        </Tag>
      </TagSet>
    </Tagging>
  3. Use cURL to call the XML API, with a PUT Bucket request and tagging query string parameter, replacing [VALUES_IN_BRACKETS] with the appropriate values:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

What's next

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

Send feedback about...

Cloud Storage
Need help? Visit our support page.