Storage Control List Managed Folders

Storage Control List Managed Folders

Explore further

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

Code sample

C++

For more information, see the Cloud Storage C++ API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  namespace 
  
 storagecontrol 
  
 = 
  
 google 
 :: 
 cloud 
 :: 
 storagecontrol_v2 
 ; 
 []( 
 storagecontrol 
 :: 
 StorageControlClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 bucket_name 
 ) 
  
 { 
  
 auto 
  
 const 
  
 parent 
  
 = 
  
 std 
 :: 
 string 
 { 
 "projects/_/buckets/" 
 } 
  
 + 
  
 bucket_name 
 ; 
  
 for 
  
 ( 
 auto 
  
 managed_folder 
  
 : 
  
 client 
 . 
 ListManagedFolders 
 ( 
 parent 
 )) 
  
 { 
  
 if 
  
 ( 
 ! 
 managed_folder 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 managed_folder 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 managed_folder 
 - 
> name 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 } 
  
 std 
 :: 
 cout 
 << 
 bucket_name 
 << 
 std 
 :: 
 endl 
 ; 
 } 
 

C#

For more information, see the Cloud Storage C# API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  using 
  
  Google.Cloud.Storage.Control.V2 
 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.Collections.Generic 
 ; 
 public 
  
 class 
  
 StorageControlListManagedFoldersSample 
 { 
  
 public 
  
 IEnumerable<ManagedFolder> 
  
 StorageControlListManagedFolders 
 ( 
 string 
  
 bucketId 
  
 = 
  
 "your-unique-bucket-name" 
 ) 
  
 { 
  
  StorageControlClient 
 
  
 storageControl 
  
 = 
  
  StorageControlClient 
 
 . 
  Create 
 
 (); 
  
 // Use "_" for project ID to signify globally scoped bucket 
  
  BucketName 
 
  
 bucketResourceName 
  
 = 
  
 new 
  
  BucketName 
 
 ( 
 "_" 
 , 
  
 bucketId 
 ); 
  
 var 
  
 managedFolders 
  
 = 
  
 storageControl 
 . 
  ListManagedFolders 
 
 ( 
 bucketResourceName 
 ); 
  
 foreach 
  
 ( 
 var 
  
 managedFolder 
  
 in 
  
 managedFolders 
 ) 
  
 { 
  
 Console 
 . 
 Write 
 ( 
 managedFolder 
 . 
 Name 
 ); 
  
 } 
  
 return 
  
 managedFolders 
 ; 
  
 } 
 } 
 

Go

For more information, see the Cloud Storage Go API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "time" 
  
 control 
  
 "cloud.google.com/go/storage/control/apiv2" 
  
 "cloud.google.com/go/storage/control/apiv2/controlpb" 
  
 "google.golang.org/api/iterator" 
 ) 
 // listManagedFolders lists all managed folders present in the bucket. 
 func 
  
 listManagedFolders 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 bucket 
  
 string 
 ) 
  
 error 
  
 { 
  
 // bucket := "bucket-name" 
  
 // folder := "managed-folder-name" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 control 
 . 
 NewStorageControlClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "NewStorageControlClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
  Close 
 
 () 
  
 ctx 
 , 
  
 cancel 
  
 := 
  
 context 
 . 
 WithTimeout 
 ( 
 ctx 
 , 
  
 time 
 . 
 Second 
 * 
 30 
 ) 
  
 defer 
  
 cancel 
 () 
  
 // Construct bucket path for a bucket containing folders. 
  
 bucketPath 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/_/buckets/%v" 
 , 
  
 bucket 
 ) 
  
 // List all folders present. 
  
 req 
  
 := 
  
& controlpb 
 . 
 ListManagedFoldersRequest 
 { 
  
 Parent 
 : 
  
 bucketPath 
 , 
  
 } 
  
 it 
  
 := 
  
 client 
 . 
 ListManagedFolders 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 for 
  
 { 
  
 f 
 , 
  
 err 
  
 := 
  
 it 
 . 
 Next 
 () 
  
 if 
  
 err 
  
 == 
  
 iterator 
 . 
  Done 
 
  
 { 
  
 break 
  
 } 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "ListManagedFolders(%q): %w" 
 , 
  
 bucketPath 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "got managed folder %v\n" 
 , 
  
 f 
 . 
  Name 
 
 ) 
  
 } 
  
 return 
  
 nil 
 } 
 

Java

For more information, see the Cloud Storage Java API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  import 
  
 com.google.storage.control.v2. BucketName 
 
 ; 
 import 
  
 com.google.storage.control.v2. ListManagedFoldersRequest 
 
 ; 
 import 
  
 com.google.storage.control.v2. ManagedFolder 
 
 ; 
 import 
  
 com.google.storage.control.v2. StorageControlClient 
 
 ; 
 class 
 ListManagedFolders 
  
 { 
  
 public 
  
 static 
  
 void 
  
 managedFolderList 
 ( 
 String 
  
 bucketName 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // Instantiates a client in a try-with-resource to automatically cleanup underlying resources 
  
 try 
  
 ( 
  StorageControlClient 
 
  
 storageControlClient 
  
 = 
  
  StorageControlClient 
 
 . 
 create 
 ()) 
  
 { 
  
  ListManagedFoldersRequest 
 
  
 listManagedFoldersRequest 
  
 = 
  
  ListManagedFoldersRequest 
 
 . 
 newBuilder 
 () 
  
 // Set project to "_" to signify global bucket 
  
 . 
 setParent 
 ( 
  BucketName 
 
 . 
 format 
 ( 
 "_" 
 , 
  
 bucketName 
 )) 
  
 . 
 build 
 (); 
  
 Iterable<ManagedFolder> 
  
 managedFolders 
  
 = 
  
 storageControlClient 
 . 
 listManagedFolders 
 ( 
 listManagedFoldersRequest 
 ). 
 iterateAll 
 (); 
  
 for 
  
 ( 
  ManagedFolder 
 
  
 folder 
  
 : 
  
 managedFolders 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "%s bucket has managed folder %s%n" 
 , 
  
 bucketName 
 , 
  
 folder 
 . 
 getName 
 ()); 
  
 } 
  
 } 
  
 } 
 } 
 

Node.js

For more information, see the Cloud Storage Node.js API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  /** 
 * TODO(developer): Uncomment these variables before running the sample. 
 */ 
 // The name of your GCS bucket 
 // const bucketName = 'bucketName'; 
 // Imports the Control library 
 const 
  
 { 
 StorageControlClient 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/storage-control 
' 
 ). 
 v2 
 ; 
 // Instantiates a client 
 const 
  
 controlClient 
  
 = 
  
 new 
  
  StorageControlClient 
 
 (); 
 async 
  
 function 
  
 callListManagedFolders 
 () 
  
 { 
  
 const 
  
 bucketPath 
  
 = 
  
 controlClient 
 . 
  bucketPath 
 
 ( 
 '_' 
 , 
  
 bucketName 
 ); 
  
 // Create the request 
  
 const 
  
 request 
  
 = 
  
 { 
  
 parent 
 : 
  
 bucketPath 
 , 
  
 }; 
  
 // Run request 
  
 const 
  
 [ 
 folders 
 ] 
  
 = 
  
 await 
  
 controlClient 
 . 
 listManagedFolders 
 ( 
 request 
 ); 
  
 for 
  
 ( 
 const 
  
 curManagedFolder 
  
 of 
  
 folders 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
 curManagedFolder 
 . 
 name 
 ); 
  
 } 
 } 
 callListManagedFolders 
 (); 
 

PHP

For more information, see the Cloud Storage PHP API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; 
 use Google\Cloud\Storage\Control\V2\ListManagedFoldersRequest; 
 /** 
 * List folders in an existing bucket. 
 * 
 * @param string $bucketName The name of your Cloud Storage bucket. 
 *        (e.g. 'my-bucket') 
 */ 
 function managed_folders_list(string $bucketName): void 
 { 
 $storageControlClient = new StorageControlClient(); 
 // Set project to "_" to signify global bucket 
 $formattedName = $storageControlClient->bucketName('_', $bucketName); 
 $request = new ListManagedFoldersRequest([ 
 'parent' => $formattedName, 
 ]); 
 $folders = $storageControlClient->listManagedFolders($request); 
 foreach ($folders as $folder) { 
 printf('%s bucket has managed folder %s' . PHP_EOL, $bucketName, $folder->getName()); 
 } 
 } 
 

Python

For more information, see the Cloud Storage Python API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  from 
  
 google.cloud 
  
 import 
  storage_control_v2 
 
 def 
  
 list_managed_folders 
 ( 
 bucket_name 
 : 
 str 
 = 
 "your-bucket-name" 
 ) 
 - 
> None 
 : 
  
 """Lists all managed folders in a Google Cloud Storage bucket. 
 Args: 
 bucket_name: The name of the Google Cloud Storage bucket. 
 Returns: 
 None. The function prints the name of each managed folder to the 
 console. 
 Example: 
 >>> list_managed_folders(bucket_name="my-test-bucket") 
 Managed folders in bucket projects/_/buckets/my-test-bucket: 
 projects/_/buckets/my-test-bucket/managedFolders/folder-one 
 projects/_/buckets/my-test-bucket/managedFolders/folder-two 
 """ 
 client 
 = 
  storage_control_v2 
 
 . 
  StorageControlClient 
 
 () 
 # The storage bucket path uses the global access pattern, 
 # in which the "_" denotes this bucket exists in the global namespace. 
 GLOBAL_NAMESPACE_PATTERN 
 = 
 "_" 
 bucket_resource_name 
 = 
 f 
 "projects/ 
 { 
 GLOBAL_NAMESPACE_PATTERN 
 } 
 /buckets/ 
 { 
 bucket_name 
 } 
 " 
 managed_folders 
 = 
 client 
 . 
  list_managed_folders 
 
 ( 
 parent 
 = 
 bucket_resource_name 
 ) 
 print 
 ( 
 f 
 "Managed folders in bucket 
 { 
 bucket_resource_name 
 } 
 :" 
 ) 
 for 
 managed_folder 
 in 
 managed_folders 
 : 
 print 
 ( 
 f 
 " 
 \t 
 { 
 managed_folder 
 . 
 name 
 } 
 " 
 ) 
 

Ruby

For more information, see the Cloud Storage Ruby API reference documentation .

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  def 
  
 list_managed_folders 
  
 bucket_name 
 : 
  
 # The ID of your GCS bucket 
  
 # bucket_name = "your-unique-bucket-name" 
  
 require 
  
 "google/cloud/storage/control" 
  
 storage_control 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
 Storage 
 :: 
 Control 
 . 
 storage_control 
  
 # The storage bucket path uses the global access pattern, in which the "_" 
  
 # denotes this bucket exists in the global namespace. 
  
 bucket_path 
  
 = 
  
 storage_control 
 . 
 bucket_path 
  
 project 
 : 
  
 "_" 
 , 
  
 bucket 
 : 
  
 bucket_name 
  
 request 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
 Storage 
 :: 
 Control 
 :: 
 V2 
 :: 
 ListManagedFoldersRequest 
 . 
 new 
  
 parent 
 : 
  
 bucket_path 
  
 managed_folders 
  
 = 
  
 storage_control 
 . 
 list_managed_folders 
  
 request 
  
 managed_folders 
 . 
 each 
  
 do 
  
 | 
 managed_folder 
 | 
  
 puts 
  
 managed_folder 
  
 end 
  
 puts 
  
 "Listed managed folders in bucket 
 #{ 
 bucket_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: