List buckets using Amazon S3 SDKs

List Cloud Storage buckets that use Amazon Simple Storage Service (Amazon S3) SDKs by using the Cloud Storage XML API with HMAC credentials.

Explore further

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

Code sample

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" 
  
 "github.com/aws/aws-sdk-go/aws" 
  
 "github.com/aws/aws-sdk-go/aws/credentials" 
  
 "github.com/aws/aws-sdk-go/aws/session" 
  
 "github.com/aws/aws-sdk-go/service/s3" 
 ) 
 func 
  
 listGCSBuckets 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 googleAccessKeyID 
  
 string 
 , 
  
 googleAccessKeySecret 
  
 string 
 ) 
  
 error 
  
 { 
  
 // googleAccessKeyID := "Your Google Access Key ID" 
  
 // googleAccessKeySecret := "Your Google Access Key Secret" 
  
 // Create a new client and do the following: 
  
 // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint. 
  
 // 2. Use Cloud Storage HMAC Credentials. 
  
 sess 
  
 := 
  
 session 
 . 
 Must 
 ( 
 session 
 . 
 NewSession 
 ( 
& aws 
 . 
 Config 
 { 
  
 Region 
 : 
  
 aws 
 . 
 String 
 ( 
 "auto" 
 ), 
  
 Endpoint 
 : 
  
 aws 
 . 
 String 
 ( 
 "https://storage.googleapis.com" 
 ), 
  
 Credentials 
 : 
  
 credentials 
 . 
 NewStaticCredentials 
 ( 
 googleAccessKeyID 
 , 
  
 googleAccessKeySecret 
 , 
  
 "" 
 ), 
  
 })) 
  
 client 
  
 := 
  
 s3 
 . 
 New 
 ( 
 sess 
 ) 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 ctx 
 , 
  
 cancel 
  
 := 
  
 context 
 . 
 WithTimeout 
 ( 
 ctx 
 , 
  
 time 
 . 
 Second 
 * 
 10 
 ) 
  
 defer 
  
 cancel 
 () 
  
 result 
 , 
  
 err 
  
 := 
  
 client 
 . 
 ListBucketsWithContext 
 ( 
 ctx 
 , 
  
& s3 
 . 
 ListBucketsInput 
 {}) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "ListBucketsWithContext: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Buckets:" 
 ) 
  
 for 
  
 _ 
 , 
  
 b 
  
 := 
  
 range 
  
 result 
 . 
 Buckets 
  
 { 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "%s\n" 
 , 
  
 aws 
 . 
 StringValue 
 ( 
 b 
 . 
 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.amazonaws.auth.AWSStaticCredentialsProvider 
 ; 
 import 
  
 com.amazonaws.auth.BasicAWSCredentials 
 ; 
 import 
  
 com.amazonaws.client.builder.AwsClientBuilder 
 ; 
 import 
  
 com.amazonaws.services.s3.AmazonS3 
 ; 
 import 
  
 com.amazonaws.services.s3.AmazonS3ClientBuilder 
 ; 
 import 
  
 com.amazonaws.services.s3.model.Bucket 
 ; 
 import 
  
 java.util.List 
 ; 
 public 
  
 class 
 ListGcsBuckets 
  
 { 
  
 public 
  
 static 
  
 void 
  
 listGcsBuckets 
 ( 
 String 
  
 googleAccessKeyId 
 , 
  
 String 
  
 googleAccessKeySecret 
 ) 
  
 { 
  
 // String googleAccessKeyId = "your-google-access-key-id"; 
  
 // String googleAccessKeySecret = "your-google-access-key-secret"; 
  
 // Create a BasicAWSCredentials using Cloud Storage HMAC credentials. 
  
 BasicAWSCredentials 
  
 googleCreds 
  
 = 
  
 new 
  
 BasicAWSCredentials 
 ( 
 googleAccessKeyId 
 , 
  
 googleAccessKeySecret 
 ); 
  
 // Create a new client and do the following: 
  
 // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint. 
  
 // 2. Use Cloud Storage HMAC Credentials. 
  
 AmazonS3 
  
 interopClient 
  
 = 
  
 AmazonS3ClientBuilder 
 . 
 standard 
 () 
  
 . 
 withEndpointConfiguration 
 ( 
  
 new 
  
 AwsClientBuilder 
 . 
 EndpointConfiguration 
 ( 
  
 "https://storage.googleapis.com" 
 , 
  
 "auto" 
 )) 
  
 . 
 withCredentials 
 ( 
 new 
  
 AWSStaticCredentialsProvider 
 ( 
 googleCreds 
 )) 
  
 . 
 build 
 (); 
  
 // Call GCS to list current buckets 
  
 List<Bucket> 
  
 buckets 
  
 = 
  
 interopClient 
 . 
 listBuckets 
 (); 
  
 // Print bucket names 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Buckets:" 
 ); 
  
 for 
  
 ( 
 Bucket 
  
 bucket 
  
 : 
  
 buckets 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 bucket 
 . 
 getName 
 ()); 
  
 } 
  
 // Explicitly clean up client resources. 
  
 interopClient 
 . 
 shutdown 
 (); 
  
 } 
 

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 .

  import 
  
 boto3 
 # type: ignore 
 def 
  
 list_gcs_buckets 
 ( 
 google_access_key_id 
 : 
 str 
 , 
 google_access_key_secret 
 : 
 str 
 ) 
 - 
> List 
 [ 
 str 
 ]: 
  
 """Lists all Cloud Storage buckets using AWS SDK for Python (boto3) 
 Positional arguments: 
 google_access_key_id: hash-based message authentication code (HMAC) access ID 
 google_access_key_secret: HMAC access secret 
 Returned value is a list of strings, one for each bucket name. 
 To use this sample: 
 1. Create a Cloud Storage HMAC key: https://cloud.google.com/storage/docs/authentication/managing-hmackeys#create 
 2. Change endpoint_url to a Google Cloud Storage XML API endpoint. 
 To learn more about HMAC: https://cloud.google.com/storage/docs/authentication/hmackeys#overview 
 """ 
 client 
 = 
 boto3 
 . 
 client 
 ( 
 "s3" 
 , 
 region_name 
 = 
 "auto" 
 , 
 endpoint_url 
 = 
 "https://storage.googleapis.com" 
 , 
 aws_access_key_id 
 = 
 google_access_key_id 
 , 
 aws_secret_access_key 
 = 
 google_access_key_secret 
 , 
 ) 
 # Call GCS to list current buckets 
 response 
 = 
 client 
 . 
 list_buckets 
 () 
 # Return list of bucket names 
 results 
 = 
 [] 
 for 
 bucket 
 in 
 response 
 [ 
 "Buckets" 
 ]: 
 results 
 . 
 append 
 ( 
 bucket 
 [ 
 "Name" 
 ]) 
 print 
 ( 
 bucket 
 [ 
 "Name" 
 ]) 
 # Can remove if not needed after development 
 return 
 results 
 

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: