This page describes how to configure your bucket to send notifications about object changes to a Pub/Sub topic. For information on subscribing to a Pub/Sub topic that receives notifications, see Choose a subscription type.
Before you begin
Before using this feature, you should:
Enable the Pub/Sub API for the project that will receive notifications.
Have
storage.buckets.update
andstorage.buckets.get
permission on the bucket you wish to monitor. See Using IAM Permissions for instructions on how to do this. If you own the project that contains the bucket, you most likely already have the necessary permission.Have sufficient permissions on the project that will receive notifications:
If you own the project that will receive notifications, you most likely have the necessary permission.
If you plan to create topics for receiving notifications, you should have
pubsub.topics.create
permission.Whether you plan to use new or existing topics, you should have
pubsub.topics.setIamPolicy
permission. If you create a topic, you typically havepubsub.topics.setIamPolicy
for it.See Pub/Sub Access Control for instructions on how to get these Pub/Sub permissions.
-
Have an existing Pub/Sub topic that you wish to send notifications to.
Get the email address of the service agent associated with the project that contains your Cloud Storage bucket.
Use the email address that you obtained in the previous step to give the service agent the IAM role
pubsub.publisher
for the desired Pub/Sub topic.
Apply a notification configuration
The following steps add a notification configuration to your bucket that sends notifications for all supported events.
Console
You cannot manage Pub/Sub Notifications with the Google Cloud console. Use the gcloud CLI or one of the available client libraries instead.
Command line
gcloud
Use the gcloud storage buckets notifications create
command:
gcloud storage buckets notifications create gs://BUCKET_NAME --topic=TOPIC_NAME
Where:
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.TOPIC_NAME
is the Pub/Sub topic to send notifications to. If you specify a topic that doesn't exist in your project, the command creates one for you.
To send notifications for a subset of events, include the
--event-types
flag.
gsutil
Use the gsutil notification create
command:
gsutil notification create -t TOPIC_NAME -f json gs://BUCKET_NAME
Where:
TOPIC_NAME
is the Pub/Sub topic to send notifications to. If you specify a topic that doesn't exist in your project, the command creates one for you.BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
To send notifications for a subset of events, include the
-e
flag.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Terraform
You can use a Terraform resource to add a notification configuration to a bucket.
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Create a JSON file that contains the following information:
{ "topic": "projects/PROJECT_ID/topics/TOPIC_NAME", "payload_format": "JSON_API_V1" }
Where:
PROJECT_ID
is the ID for the project associated with the Pub/Sub topic that you want to send notifications to. For example,my-pet-project
.TOPIC_NAME
is the Pub/Sub topic to send notifications to. For example,my-topic
.
To send notifications for a subset of events, include the
event_types
field in the body of your JSON request.Use
cURL
to call the JSON API with aPOST notificationConfigs
request:curl -X POST --data-binary @JSON_FILE_NAME \ -H "Authorization: Bearer OAUTH2_TOKEN" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs"
Where:
JSON_FILE_NAME
is the path for the file that you created in Step 2.OAUTH2_TOKEN
is the access token you generated in Step 1.BUCKET_NAME
is the name of the bucket you want notifications to be generated for. For example,my-bucket
.
XML API
You cannot manage Pub/Sub Notifications with the XML API.
Get a notification configuration
To get a specific notification configuration that's associated with your bucket:
Console
You cannot manage Pub/Sub Notifications with the Google Cloud console. Use the gcloud CLI or one of the available client libraries instead.
Command line
gcloud
Use the gcloud storage buckets notifications describe
command:
gcloud storage buckets notifications describe projects/_/buckets/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID
Where:
BUCKET_NAME
is the name of the bucket whose notification configuration you want to retrieve For example,my-bucket
.NOTIFICATION_ID
is the ID number of the desired configuration. For example,5
.
If successful, the response looks similar to the following example:
etag: '132' id: '132' kind: storage#notification payload_format: JSON_API_V1 selfLink: https://www.googleapis.com/storage/v1/b/my-bucket/notificationConfigs/132 topic: //pubsub.googleapis.com/projects/my-project/topics/my-bucket
gsutil
You cannot get an individual notification configuration with gsutil. Use the gcloud CLI instead.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Use
cURL
to call the JSON API with aGET notificationConfigs
request:curl -X GET \ -H "Authorization: Bearer OAUTH2_TOKEN" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID"
Where:
OAUTH2_TOKEN
is the access token you generated in Step 1.BUCKET_NAME
is the name of the bucket whose notification configuration you want to retrieve. For example,my-bucket
.NOTIFICATION_ID
is the ID number of the notification configuration you want to retrieve. For example,5
.
XML API
You cannot manage Pub/Sub Notifications with the XML API.
List notification configurations for a bucket
To list all the notification configurations associated with a particular bucket:
Console
You cannot manage Pub/Sub Notifications with the Google Cloud console. Use the gcloud CLI or one of the available client libraries instead.
Command line
gcloud
Use the gcloud storage buckets notifications list
command:
gcloud storage buckets notifications list gs://BUCKET_NAME
Where BUCKET_NAME
is the name of the bucket
whose notification configurations you want to list. For example,
my-bucket
.
gsutil
Use the gsutil notification list
command:
gsutil notification list gs://BUCKET_NAME
Where BUCKET_NAME
is the name of the relevant
bucket. For example, my-bucket
.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Use
cURL
to call the JSON API with aGET notificationConfigs
request:curl -X GET \ -H "Authorization: Bearer OAUTH2_TOKEN" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs"
Where:
OAUTH2_TOKEN
is the access token you generated in Step 1.BUCKET_NAME
is the name of the bucket whose notification configurations you want to list. For example,my-bucket
.
XML API
You cannot manage Pub/Sub Notifications with the XML API.
Remove a notification configuration
To remove an existing notification configuration from your bucket:
Console
You cannot manage Pub/Sub Notifications with the Google Cloud console. Use the gcloud CLI or one of the available client libraries instead.
Command line
gcloud
Use the gcloud storage buckets notifications delete
command:
gcloud storage buckets notifications delete projects/_/buckets/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID
Where:
BUCKET_NAME
is the name of the bucket whose notification configuration you want to delete. For example,my-bucket
.NOTIFICATION_ID
is the ID number of the configuration you want to delete. For example,5
.
If successful, the response looks similar to the following example:
Completed 1
Once sent, it may take up to 30 seconds for all notifications triggered by the notification configuration to stop.
gsutil
Use the gsutil notification delete
command:
gsutil notification delete projects/_/buckets/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID
Where:
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.NOTIFICATION_ID
is the ID number of the configuration you want to delete. For example,5
.
Once sent, it may take up to 30 seconds for all notifications triggered by the notification configuration to stop.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Terraform
To remove the notification configuration you created, run
terraform destroy
from the folder containing your
Terraform file.
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Use
cURL
to call the JSON API with aDELETE notificationConfigs
request:curl -X DELETE \ -H "Authorization: Bearer OAUTH2_TOKEN" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/notificationConfigs/NOTIFICATION_ID"
Where:
OAUTH2_TOKEN
is the access token you generated in Step 1.BUCKET_NAME
is the name of the bucket whose notification configuration you want to delete. For example,my-bucket
.NOTIFICATION_ID
is the ID number of the notification configuration you want to delete. For example,5
.
Once sent, it may take up to 30 seconds for all notifications triggered by the notification configuration to stop.
XML API
You cannot manage Pub/Sub Notifications with the XML API.
What's next
- Learn more about Pub/Sub notifications for Cloud Storage.
- Create a subscription in Pub/Sub to view notifications sent by Cloud Storage.
- See a Python example app of polling for notifications on GitHub.
- Use Cloud Functions to deliver events with a Cloud Storage trigger.