This page provides an overview of Pub/Sub notifications for Cloud Storage.
Pub/Sub notifications sends information about changes to objects in your buckets to Pub/Sub, where the information is added to a Pub/Sub topic of your choice in the form of messages. For example, you can track objects that are created and deleted in your bucket. Each notification contains information describing both the event that triggered it and the object that changed.
You can send notifications to any Pub/Sub topic in any project for which you have sufficient permissions. Once received by the Pub/Sub topic, subscribers to the topic can receive the associated message. See Prerequisites for information on connecting your Cloud Storage buckets to a Pub/Sub topic.
Other notification options
Subscribing to Pub/Sub notifications is a versatile way to trigger alerts and actions in response to changes in a bucket. The following options are also available:
Cloud Functions: If you only want to trigger a lightweight, stand-alone function in response to events and don't want to manage a Pub/Sub topic, use Cloud Functions. Cloud Functions allow you to execute C#, Go, Java, Node.js, Python, PHP, and Ruby functions when an object in your bucket changes. Note that your bucket must reside in the same project as Cloud Functions. See the associated tutorial for a demonstration of using Cloud Functions with Cloud Storage.
Object change notification: Object change notification is a separate, older feature within Cloud Storage for generating notifications. This feature sends HTTPS messages to a client application that you've set up separately. This feature is generally not recommended, because Pub/Sub notifications are cheaper, easier to use, and more flexible.
A notification configuration is a rule you attach to a bucket that specifies:
- The topic in Pub/Sub that receives notifications.
- The events that trigger a notification to be sent.
- The information contained within notifications.
You can attach multiple notification configurations to a bucket. A bucket can have up to 100 total notification configurations and up to 10 notification configurations set to trigger for a specific event.
For example, if you have a notification configuration that sends deletion notifications to one Pub/Sub topic, you can add a second notification configuration to the bucket that sends deletion notifications to another topic. However, if you try to create more than 10 notification configurations that do this, you receive an error. In addition to these notification configurations, you can also create notification configurations that send notifications for other events, such as object creation, either to Pub/Sub topics used by the deletion notifications, or to different topics.
Each notification configuration is identified by an integer. This integer is returned:
- When you create the notification configuration.
- When you list the notification configurations attached to a bucket.
- In the
notificationConfigattribute of each notification triggered by the notification configuration.
Creating and deleting notification configurations increment a bucket's metageneration number.
The following is a list of event types currently supported by Cloud Storage:
||Sent when a new object (or a new generation of an existing object) is successfully created in the bucket. This includes copying or rewriting an existing object. A failed upload does not trigger this event.|
||Sent when the metadata of an existing object changes.|
||Sent when an object has been permanently deleted. This includes objects that are replaced or deleted as part of the bucket's lifecycle configuration. This does not include objects that become noncurrent (see
||Only sent when a bucket has enabled object versioning. This event indicates that the live version of an object has become a noncurrent version, either because it was explicitly made noncurrent or because it was replaced by the upload of an object of the same name.|
Replacing an existing object with a new one of the same name triggers two
OBJECT_FINALIZE for the new version of the object and either
OBJECT_DELETE for the replaced object. The
OBJECT_FINALIZE event contains an additional attribute
which provides the generation number of the object that was replaced. The
OBJECT_DELETE event contains an additional attribute
overwrittenByGeneration, which provides the generation number of the new
Notifications sent to the Pub/Sub topic consist of two parts:
- Attributes: A set of key:value pairs describing the event.
- Payload: A string that contains the metadata of the changed object.
Attributes are key:value pairs contained in all notifications sent by Cloud Storage to your Pub/Sub topic. Notifications always contain the following set of key:value pairs, regardless of the notification's payload:
||An identifier for the notification configuration that triggered this notification.|
||The type of event that has just occurred. See Event types for a list of possible values.|
||The format of the object payload. See Payload for a list of possible values.|
||The name of the bucket that contains the changed object.|
||The name of the changed object.|
||The generation number of the changed object.|
||The time that the event took place, expressed in the RFC 3339 format.|
Notifications sometimes contain the following set of key:value pairs, regardless of the notification's payload:
||The generation number of the object that replaced the object that this notification pertains to. This attribute only appears in
||The generation number of the object that was replaced by the object that this notification pertains to. This attribute only appears in
The following key:value pair is deprecated. It does not appear for new subscriptions and will not appear in any subscription after June 1, 2018:
||The path of the Cloud Storage object that changed.|
In addition to the above attributes, a notification configuration can contain
up to 10 custom attributes. Custom attributes are defined when creating a
notification configuration, using the
--custom-attributes in a
gcloud storage command, or the
-m flag in a
gsutil notification command, or the
custom_attributes object in the
body of a
POST notificationConfigs JSON request.
The payload is a string that contains the metadata of the changed object. When you create a notification configuration, you specify a type of payload to include in notifications triggered by that configuration. You can specify the following types of payload:
|NONE||No payload is included with the notification.|
|JSON_API_V1||The payload will be a UTF-8 string containing the resource representation of the object’s metadata.|
OBJECT_DELETE notifications, the metadata contained in the payload represents
the object metadata as it was before the delete, along with an additional
timeDeleted property. For all other notifications, the metadata included in the
payload represents the object metadata after the
For example, say you have a notification configuration that
OBJECT_METADATA_UPDATE events. If a user changes the
property of an object from
OBJECT_METADATA_UPDATE notification is sent, and the metadata in the
When you add a notification configuration, Cloud Storage may take up to 30 seconds to begin sending notifications associated with it. Once started, Cloud Storage guarantees at-least-once delivery to Pub/Sub. Pub/Sub also offers at-least-once delivery to the recipient, which means that you could receive multiple messages, with multiple IDs, that represent the same Cloud Storage event.
Notifications are not guaranteed to be published in the order Pub/Sub receives them. If you plan to modify the Cloud Storage object based on a notification, it is recommended that you use the object's generation and metageneration numbers as preconditions on your update request.
If a notification consistently cannot be delivered to a Pub/Sub topic, Cloud Storage may delete the notification after 7 days. Delivery failure can occur when the Pub/Sub topic no longer exists, when Cloud Storage no longer has permission to publish to the topic, or when the project that owns the topic exceeds its publishing quota.
- Configure Pub/Sub notifications for Cloud Storage.
- Learn more about Pub/Sub.
- See a Python example app of polling for notifications on GitHub.
- Use Cloud Functions to deliver events with a Cloud Storage trigger.
- Use Pub/Sub notifications for event-driven transfers between Cloud Storage buckets.