使用 Amazon S3 SDK 列出值區
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Cloud Storage XML API 和 HMAC 憑證,列出使用 Amazon Simple Storage Service (Amazon S3) SDK 的 Cloud Storage 值區。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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)."]]