List subscriptions in project

Lists subscriptions in a project.

Explore further

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

Code sample

C++

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

  namespace 
  
 pubsub_admin 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub_admin 
 ; 
 []( 
 pubsub_admin 
 :: 
 SubscriptionAdminClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 ) 
  
 { 
  
 int 
  
 count 
  
 = 
  
 0 
 ; 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 ListSubscriptionsRequest 
  
 request 
 ; 
  
 request 
 . 
 set_project 
 ( 
 google 
 :: 
 cloud 
 :: 
 Project 
 ( 
 project_id 
 ). 
 FullName 
 ()); 
  
 for 
  
 ( 
 auto 
&  
 subscription 
  
 : 
  
 client 
 . 
 ListSubscriptions 
 ( 
 request 
 )) 
  
 { 
  
 if 
  
 ( 
 ! 
 subscription 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 subscription 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "Subscription Name: " 
 << 
 subscription 
 - 
> name 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 ++ 
 count 
 ; 
  
 } 
  
 if 
  
 ( 
 count 
  
 == 
  
 0 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "No subscriptions found in project " 
 << 
 project_id 
 << 
 " 
 \n 
 " 
 ; 
  
 } 
 } 
 

C#

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

  using 
  
  Google.Api.Gax.ResourceNames 
 
 ; 
 using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 using 
  
 System.Collections.Generic 
 ; 
 public 
  
 class 
  
 ListSubscriptionsSample 
 { 
  
 public 
  
 IEnumerable<Subscription> 
  
 ListSubscriptions 
 ( 
 string 
  
 projectId 
 ) 
  
 { 
  
  SubscriberServiceApiClient 
 
  
 subscriber 
  
 = 
  
  SubscriberServiceApiClient 
 
 . 
  Create 
 
 (); 
  
  ProjectName 
 
  
 projectName 
  
 = 
  
  ProjectName 
 
 . 
  FromProject 
 
 ( 
 projectId 
 ); 
  
 var 
  
 subscriptions 
  
 = 
  
 subscriber 
 . 
  ListSubscriptions 
 
 ( 
 projectName 
 ); 
  
 return 
  
 subscriptions 
 ; 
  
 } 
 } 
 

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" 
  
 "cloud.google.com/go/pubsub/v2" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
  
 "google.golang.org/api/iterator" 
 ) 
 func 
  
 list 
 ( 
 projectID 
  
 string 
 ) 
  
 ([] 
 * 
 pubsubpb 
 . 
 Subscription 
 , 
  
 error 
 ) 
  
 { 
  
 // projectID := "my-project-id" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 var 
  
 subs 
  
 [] 
 * 
 pubsubpb 
 . 
 Subscription 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 ListSubscriptionsRequest 
 { 
  
 Project 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s" 
 , 
  
 projectID 
 ), 
  
 } 
  
 it 
  
 := 
  
 client 
 . 
 SubscriptionAdminClient 
 . 
 ListSubscriptions 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 for 
  
 { 
  
 s 
 , 
  
 err 
  
 := 
  
 it 
 . 
 Next 
 () 
  
 if 
  
 err 
  
 == 
  
 iterator 
 . 
 Done 
  
 { 
  
 break 
  
 } 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "Next: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 subs 
  
 = 
  
 append 
 ( 
 subs 
 , 
  
 s 
 ) 
  
 } 
  
 return 
  
 subs 
 , 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Java 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 
  
 com.google.cloud.pubsub.v1. SubscriptionAdminClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. ProjectName 
 
 ; 
 import 
  
 com.google.pubsub.v1. Subscription 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 ListSubscriptionsInProjectExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 listSubscriptionInProjectExample 
 ( 
 projectId 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 listSubscriptionInProjectExample 
 ( 
 String 
  
 projectId 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 try 
  
 ( 
  SubscriptionAdminClient 
 
  
 subscriptionAdminClient 
  
 = 
  
  SubscriptionAdminClient 
 
 . 
 create 
 ()) 
  
 { 
  
  ProjectName 
 
  
 projectName 
  
 = 
  
  ProjectName 
 
 . 
 of 
 ( 
 projectId 
 ); 
  
 for 
  
 ( 
  Subscription 
 
  
 subscription 
  
 : 
  
 subscriptionAdminClient 
 . 
 listSubscriptions 
 ( 
 projectName 
 ). 
 iterateAll 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 subscription 
 . 
 getName 
 ()); 
  
 } 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Listed all the subscriptions in the project." 
 ); 
  
 } 
  
 } 
 } 
 

PHP

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

  use Google\Cloud\PubSub\PubSubClient; 
 /** 
 * Lists all Pub/Sub subscriptions. 
 * 
 * @param string $projectId  The Google project ID. 
 */ 
 function list_subscriptions($projectId) 
 { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 foreach ($pubsub->subscriptions() as $subscription) { 
 printf('Subscription: %s' . PHP_EOL, $subscription->name()); 
 } 
 } 
 

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 .

  pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 subscription_admin 
  
 = 
  
 pubsub 
 . 
  subscription_admin 
 
 subscriptions 
  
 = 
  
 subscription_admin 
 . 
 list_subscriptions 
  
 \ 
  
 project 
 : 
  
 pubsub 
 . 
 project_path 
 puts 
  
 "Subscriptions:" 
 subscriptions 
 . 
 each 
  
 do 
  
 | 
 subscription 
 | 
  
 puts 
  
 subscription 
 . 
 name 
 end 
 

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: