Buckets mit Amazon S3 SDKs auflisten
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Cloud Storage-Buckets, die Amazon Simple Storage Service SDKs (Amazon S3) SDKs verwenden, mit der Cloud Storage XML API mit HMAC-Anmeldedaten auflisten.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]