Using bucket labels (original) (raw)
Discover
Get started
Create buckets
Access and manage buckets
Upload and download objects
Access and manage objects
Tools to access and manage objects using a directory structure
Get insights on your stored data
Cache objects
Control data lifecycles
Make requests
Secure data
Access control
- Overview
- Bucket IP filtering
* Overview
* Create a bucket with IP filtering rules
* Create or update IP filtering rules on an existing bucket
* Get IP bucket filtering rules
* List bucket IP filtering rules
* Delete bucket IP filtering rules
* Disable bucket IP filtering
* Bypass bucket IP filtering rules - Sharing and collaboration scenarios
- Access control best practices
Monitor data and usage
Protection, backup, and recovery
Soft delete
Mount buckets with Cloud Storage FUSE
Performance
Work across products, Clouds, and platforms
Troubleshoot
Using bucket labels
This page shows you how to add, modify, remove, and view the labels set on a bucket in Cloud Storage.
Required roles
In order to get the required permissions for adding and managing bucket labels, ask your administrator to grant you the Storage Admin (roles/storage.admin) IAM role on the bucket.
This predefined role contains the permissions required to add and manage bucket labels. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
storage.buckets.getstorage.buckets.list- This permission is only required if you plan on using the Google Cloud console to perform the instructions on this page.
storage.buckets.update
You might also be able to get these permissions with custom roles.
For instructions on granting roles on buckets, seeSet and manage IAM policies on buckets.
Add, modify, or remove a bucket's labels
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
Go to Buckets - In the list of buckets, click the name of the bucket for which you want to add, modify, or remove labels.
- In the Bucket details page, click the Configuration tab.
- Click the Edit icon () for Labels.
The label editor window appears.- To add a label, click theAdd label button, and specify a
keyand avaluefor your label. - To modify an existing label, click its Value and a enter a new value.
- To remove a label, click the Trash icon associated with the label you want to remove.
- To add a label, click theAdd label button, and specify a
- Click Save.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, seeTroubleshooting.
Command line
To add a new label or update an existing label, use thegcloud storage buckets update command with the --update-labelsflag. For example:
gcloud storage buckets update gs://BUCKET_NAME --update-labels=KEY_1=VALUE_1
Where
BUCKET_NAMEis the name of the bucket that the label applies to. For example,my-bucket.KEY_1is the key name for your label. For example,pet.VALUE_1is the value for your label. For example,dog.
To remove an existing label, use the -remove-labels flag. For example:
gcloud storage buckets update gs://BUCKET_NAME --remove-labels=KEY_1
You can change multiple labels using the previous commands by including the labels in a comma-separated list within the relevant flag. For example,--update-labels=blue-key=cyan,red-key=ruby.
To remove all labels attached to a bucket, use the following command:
gcloud storage buckets update gs://BUCKET_NAME --clear-labels
Client libraries
C++
For more information, see theCloud Storage C++ API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
C#
For more information, see theCloud Storage C# API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
Go
For more information, see theCloud Storage Go API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
Java
For more information, see theCloud Storage Java API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
Node.js
For more information, see theCloud Storage Node.js API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
PHP
For more information, see theCloud Storage PHP API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
Python
For more information, see theCloud Storage Python API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
Ruby
For more information, see theCloud Storage Ruby API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
The following sample adds the specified label to a bucket, or modifies the label if it already exists for the bucket:
The following sample removes the specified label from a bucket:
REST APIs
JSON API
- Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorizationheader. - Create a JSON file that contains the following information:
{
"labels": {
"KEY_1": "VALUE_1"
}
}
WhereKEY_1is the key name for your label. For example,pet.VALUE_1is the value for your label. For example,dog. If you want to remove a key, usenullin place of"<var>VALUE_1</var>".
- Use cURL to call the JSON API with aPATCH Bucket request:
curl -X PATCH --data-binary @JSON_FILE_NAME \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=labels"
Where:JSON_FILE_NAMEis the path for the JSON file that you created in Step 2.BUCKET_NAMEis the name of the bucket that the label applies to. For example,my-bucket.
You can add, edit, or remove multiple key:value pairs in a request.
XML API
- Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorizationheader. - Create an XML file that contains the following information: KEY_1 VALUE_1 Where: * `KEY_1` is the key name for your label. For example, `pet`. * `VALUE_1` is the value for your label. For example, `dog`. Note that you can add multiple `` elements in a request. If you want to remove all labels on a bucket, use a single, empty`` element in the file:
- Use cURL to call the XML API with aPUT Bucket request and
taggingquery string parameter:
curl -X PUT --data-binary @XML_FILE_NAME \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://storage.googleapis.com/BUCKET_NAME?tagging"
Where:XML_FILE_NAMEis the path for the XML file that you created in Step 2.BUCKET_NAMEis the name of the bucket that the label applies to. For example,my-bucket.
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
Go to Buckets - Click the name of the bucket whose labels you want to view.
- Click the Configuration tab.
The labels set on the bucket are listed in the Labels field.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, seeTroubleshooting.
Command line
Use the gcloud storage buckets describe command with the--format flag:
gcloud storage buckets describe gs://BUCKET_NAME --format="default(labels)"
Where BUCKET_NAME is the name of the bucket whose labels you want to view. For example, my-bucket.
If successful and labels exist for the bucket, the response is similar to the following:
labels: red-key: ruby blue-key: cyan
If successful and labels to not exist for the bucket, the response is similar to the following:
null
Client libraries
C++
For more information, see theCloud Storage C++ API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
C#
For more information, see theCloud Storage C# API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
Go
For more information, see theCloud Storage Go API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
Java
For more information, see theCloud Storage Java API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
Node.js
For more information, see theCloud Storage Node.js API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
PHP
For more information, see theCloud Storage PHP API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
Python
For more information, see theCloud Storage Python API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
Ruby
For more information, see theCloud Storage Ruby API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
To view a bucket's labels, follow the instructions for displaying a bucket's metadata and look for the label field in the response.
REST APIs
What's next
- Learn about bucket tags.
- Get additional bucket metadata.
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.
Last updated 2025-12-15 UTC.