Create a Pub/Sub notification on a Bucket

This sample creates a Pub/Sub notification configuration on a Bucket that sends notifications for all supported events.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands . For more information, see the Terraform provider reference documentation .

  // Create a Pub/Sub notification. 
 resource 
  
 "google_storage_notification" 
  
 "notification" 
  
 { 
  
 provider 
  
 = 
  
 google-beta 
  
 bucket 
  
 = 
  
 google_storage_bucket.bucket.name 
  
 payload_format 
  
 = 
  
 "JSON_API_V1" 
  
 topic 
  
 = 
  
 google_pubsub_topic.topic.id 
  
 depends_on 
  
 = 
  
 [ 
 google_pubsub_topic_iam_binding.binding 
 ] 
 } 
 // Enable notifications by giving the correct IAM permission to the unique service account. 
 data 
  
 "google_storage_project_service_account" 
  
 "gcs_account" 
  
 { 
  
 provider 
  
 = 
  
 google-beta 
 } 
 // Create a Pub/Sub topic. 
 resource 
  
 "google_pubsub_topic_iam_binding" 
  
 "binding" 
  
 { 
  
 provider 
  
 = 
  
 google-beta 
  
 topic 
  
 = 
  
 google_pubsub_topic.topic.id 
  
 role 
  
 = 
  
 "roles/pubsub.publisher" 
  
 members 
  
 = 
  
 [ 
 "serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}" 
 ] 
 } 
 resource 
  
 "random_id" 
  
 "bucket_prefix" 
  
 { 
  
 byte_length 
  
 = 
  
 8 
 } 
 // Create a new storage bucket. 
 resource 
  
 "google_storage_bucket" 
  
 "bucket" 
  
 { 
  
 name 
  
 = 
  
 "${random_id.bucket_prefix.hex}-example-bucket-name" 
  
 provider 
  
 = 
  
 google-beta 
  
 location 
  
 = 
  
 "US" 
  
 uniform_bucket_level_access 
  
 = 
  
 true 
 } 
 resource 
  
 "google_pubsub_topic" 
  
 "topic" 
  
 { 
  
 name 
  
 = 
  
 "your_topic_name" 
  
 provider 
  
 = 
  
 google-beta 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: