List buckets using Amazon S3 SDKs
Stay organized with collections
Save and categorize content based on your preferences.
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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# List buckets using Amazon S3 SDKs\n\nList Cloud Storage buckets that use Amazon Simple Storage Service (Amazon S3) SDKs by using the Cloud Storage XML API with HMAC credentials.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Simple migration from Amazon S3 to Cloud Storage](/storage/docs/aws-simple-migration)\n\nCode sample\n-----------\n\n### Go\n\n\nFor more information, see the\n[Cloud Storage Go API\nreference documentation](https://pkg.go.dev/cloud.google.com/go/storage).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \t\"github.com/aws/aws-sdk-go/aws\"\n \t\"github.com/aws/aws-sdk-go/aws/credentials\"\n \t\"github.com/aws/aws-sdk-go/aws/session\"\n \t\"github.com/aws/aws-sdk-go/service/s3\"\n )\n\n func listGCSBuckets(w io.Writer, googleAccessKeyID string, googleAccessKeySecret string) error {\n \t// googleAccessKeyID := \"Your Google Access Key ID\"\n \t// googleAccessKeySecret := \"Your Google Access Key Secret\"\n\n \t// Create a new client and do the following:\n \t// 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.\n \t// 2. Use Cloud Storage HMAC Credentials.\n \tsess := session.Must(session.NewSession(&aws.Config{\n \t\tRegion: aws.String(\"auto\"),\n \t\tEndpoint: aws.String(\"https://storage.googleapis.com\"),\n \t\tCredentials: credentials.NewStaticCredentials(googleAccessKeyID, googleAccessKeySecret, \"\"),\n \t}))\n\n \tclient := s3.New(sess)\n \tctx := context.Background()\n\n \tctx, cancel := context.WithTimeout(ctx, time.Second*10)\n \tdefer cancel()\n \tresult, err := client.ListBucketsWithContext(ctx, &s3.ListBucketsInput{})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"ListBucketsWithContext: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Buckets:\")\n \tfor _, b := range result.Buckets {\n \t\tfmt.Fprintf(w, \"%s\\n\", aws.StringValue(b.Name))\n \t}\n\n \treturn nil\n }\n\n### Java\n\n\nFor more information, see the\n[Cloud Storage Java API\nreference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import com.amazonaws.auth.AWSStaticCredentialsProvider;\n import com.amazonaws.auth.BasicAWSCredentials;\n import com.amazonaws.client.builder.AwsClientBuilder;\n import com.amazonaws.services.s3.AmazonS3;\n import com.amazonaws.services.s3.AmazonS3ClientBuilder;\n import com.amazonaws.services.s3.model.Bucket;\n import java.util.List;\n\n public class ListGcsBuckets {\n public static void listGcsBuckets(String googleAccessKeyId, String googleAccessKeySecret) {\n\n // String googleAccessKeyId = \"your-google-access-key-id\";\n // String googleAccessKeySecret = \"your-google-access-key-secret\";\n\n // Create a BasicAWSCredentials using Cloud Storage HMAC credentials.\n BasicAWSCredentials googleCreds =\n new BasicAWSCredentials(googleAccessKeyId, googleAccessKeySecret);\n\n // Create a new client and do the following:\n // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.\n // 2. Use Cloud Storage HMAC Credentials.\n AmazonS3 interopClient =\n AmazonS3ClientBuilder.standard()\n .withEndpointConfiguration(\n new AwsClientBuilder.EndpointConfiguration(\n \"https://storage.googleapis.com\", \"auto\"))\n .withCredentials(new AWSStaticCredentialsProvider(googleCreds))\n .build();\n\n // Call GCS to list current buckets\n List\u003cBucket\u003e buckets = interopClient.listBuckets();\n\n // Print bucket names\n System.out.println(\"Buckets:\");\n for (Bucket bucket : buckets) {\n System.out.println(bucket.getName());\n }\n\n // Explicitly clean up client resources.\n interopClient.shutdown();\n }\n\n### Python\n\n\nFor more information, see the\n[Cloud Storage Python API\nreference documentation](https://cloud.google.com/python/docs/reference/storage/latest).\n\n\nTo authenticate to Cloud Storage, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/storage/docs/authentication#client-libs).\n\n import boto3 # type: ignore\n\n\n def list_gcs_buckets(\n google_access_key_id: str, google_access_key_secret: str\n ) -\u003e List[str]:\n \"\"\"Lists all Cloud Storage buckets using AWS SDK for Python (boto3)\n Positional arguments:\n google_access_key_id: hash-based message authentication code (HMAC) access ID\n google_access_key_secret: HMAC access secret\n\n Returned value is a list of strings, one for each bucket name.\n\n To use this sample:\n 1. Create a Cloud Storage HMAC key: https://cloud.google.com/storage/docs/authentication/managing-hmackeys#create\n 2. Change endpoint_url to a Google Cloud Storage XML API endpoint.\n\n To learn more about HMAC: https://cloud.google.com/storage/docs/authentication/hmackeys#overview\n \"\"\"\n client = boto3.client(\n \"s3\",\n region_name=\"auto\",\n endpoint_url=\"https://storage.googleapis.com\",\n aws_access_key_id=google_access_key_id,\n aws_secret_access_key=google_access_key_secret,\n )\n\n # Call GCS to list current buckets\n response = client.list_buckets()\n\n # Return list of bucket names\n results = []\n for bucket in response[\"Buckets\"]:\n results.append(bucket[\"Name\"])\n print(bucket[\"Name\"]) # Can remove if not needed after development\n return results\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=storage)."]]