v1 Commit a revision for a Protocol Buffer schema (DEPRECATED)

(DEPRECATED) Commit a revision for a Protocol Buffer schema

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" 
  
 "os" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 // commitProtoSchema commits a new proto schema revision to an existing schema. 
 func 
  
 commitProtoSchema 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 schemaID 
 , 
  
 protoFile 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // schemaID := "my-schema" 
  
 // protoFile = "path/to/a/proto/schema/file(.proto)/formatted/in/protocol/buffers" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewSchemaClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewSchemaClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 // Read a proto file as a byte slice. 
  
 protoSource 
 , 
  
 err 
  
 := 
  
 os 
 . 
 ReadFile 
 ( 
 protoFile 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error reading from file: %s" 
 , 
  
 protoFile 
 ) 
  
 } 
  
 config 
  
 := 
  
 pubsub 
 . 
  SchemaConfig 
 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 Type 
 : 
  
 pubsub 
 . 
  SchemaProtocolBuffer 
 
 , 
  
 Definition 
 : 
  
 string 
 ( 
 protoSource 
 ), 
  
 } 
  
 s 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CommitSchema 
 ( 
 ctx 
 , 
  
 schemaID 
 , 
  
 config 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "CommitSchema: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Committed a schema using a protobuf schema: %#v\n" 
 , 
  
 s 
 ) 
  
 return 
  
 nil 
 } 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby 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 .

  # schema_id = "your-schema-id" 
 # proto_file = "path/to/a/proto_file.proto" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 schema 
  
 = 
  
 pubsub 
 . 
 schema 
  
 schema_id 
 definition 
  
 = 
  
 File 
 . 
 read 
  
 proto_file 
 result 
  
 = 
  
 schema 
 . 
 commit 
  
 definition 
 , 
  
 :protocol_buffer 
 puts 
  
 "Schema committed with revision 
 #{ 
 result 
 . 
 revision_id 
 } 
 ." 
 result 
 

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: