Amazon S3 SDK를 사용하여 버킷 나열
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
HMAC 사용자 인증 정보와 함께 Cloud Storage XML API를 사용하여 Amazon Simple Storage Service(Amazon S3) SDK를 사용하는 Cloud Storage 버킷을 나열합니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)."]]