v1 Create a Cloud Storage subscription (DEPRECATED)

Create a Cloud Storage subscription

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "time" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 // createCloudStorageSubscription creates a Pub/Sub subscription that exports messages to Cloud Storage. 
 func 
  
 createCloudStorageSubscription 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 subID 
  
 string 
 , 
  
 topic 
  
 * 
 pubsub 
 . 
 Topic 
 , 
  
 bucket 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // subID := "my-sub" 
  
 // topic of type https://godoc.org/cloud.google.com/go/pubsub#Topic 
  
 // note bucket should not have the gs:// prefix 
  
 // bucket := "my-bucket" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 sub 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CreateSubscription 
 ( 
 ctx 
 , 
  
 subID 
 , 
  
 pubsub 
 . 
 SubscriptionConfig 
 { 
  
 Topic 
 : 
  
 topic 
 , 
  
 CloudStorageConfig 
 : 
  
 pubsub 
 . 
 CloudStorageConfig 
 { 
  
 Bucket 
 : 
  
 bucket 
 , 
  
 FilenamePrefix 
 : 
  
 "log_events_" 
 , 
  
 FilenameSuffix 
 : 
  
 ".avro" 
 , 
  
 OutputFormat 
 : 
  
& pubsub 
 . 
  CloudStorageOutputFormatAvroConfig 
 
 { 
 WriteMetadata 
 : 
  
 true 
 }, 
  
 MaxDuration 
 : 
  
 1 
  
 * 
  
 time 
 . 
 Minute 
 , 
  
 MaxBytes 
 : 
  
 1e8 
 , 
  
 }, 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "client.CreateSubscription: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Created Cloud Storage subscription: %v\n" 
 , 
  
 sub 
 ) 
  
 return 
  
 nil 
 } 
 

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: