버킷 나열
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
프로젝트의 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\n\nList the Cloud Storage buckets in a project.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Listing buckets](/storage/docs/listing-buckets)\n\nCode sample\n-----------\n\n### C++\n\n\nFor more information, see the\n[Cloud Storage C++ API\nreference documentation](/cpp/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 namespace gcs = ::google::cloud::storage;\n using ::google::cloud::StatusOr;\n [](gcs::Client client) {\n int count = 0;\n gcs::ListBucketsReader bucket_list = client.ListBuckets();\n for (auto&& bucket_metadata : bucket_list) {\n if (!bucket_metadata) throw std::move(bucket_metadata).status();\n\n std::cout \u003c\u003c bucket_metadata-\u003ename() \u003c\u003c \"\\n\";\n ++count;\n }\n\n if (count == 0) {\n std::cout \u003c\u003c \"No buckets in default project\\n\";\n }\n }\n\n### C#\n\n\nFor more information, see the\n[Cloud Storage C# API\nreference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/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\n using Google.Apis.Storage.v1.Data;\n using https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;\n using System;\n using System.Collections.Generic;\n\n public class ListBucketsSample\n {\n public IEnumerable\u003cBucket\u003e ListBuckets(string projectId = \"your-project-id\")\n {\n var storage = https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();\n var buckets = storage.ListBuckets(projectId);\n Console.WriteLine(\"Buckets:\");\n foreach (var bucket in buckets)\n {\n Console.WriteLine(bucket.Name);\n }\n return buckets;\n }\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\"cloud.google.com/go/storage\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listBuckets lists buckets in the project.\n func listBuckets(w io.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, projectID string) ([]string, error) {\n \t// projectID := \"my-project-id\"\n \tctx := context.Background()\n \tclient, err := storage.NewClient(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"storage.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tctx, cancel := context.WithTimeout(ctx, time.Second*30)\n \tdefer cancel()\n\n \tvar buckets []string\n \tit := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Buckets(ctx, projectID)\n \tfor {\n \t\tbattrs, err := it.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn nil, err\n \t\t}\n \t\tbuckets = append(buckets, battrs.Name)\n \t\tfmt.Fprintf(w, \"Bucket: %v\\n\", battrs.Name)\n \t}\n \treturn buckets, 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.google.api.gax.paging.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.paging.Page.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;\n import com.google.cloud.storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;\n\n public class ListBuckets {\n public static void listBuckets(String projectId) {\n // The ID of your GCP project\n // String projectId = \"your-project-id\";\n\n https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();\n Page\u003cBucket\u003e buckets = storage.https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_list_com_google_cloud_storage_Storage_BucketListOption____();\n\n for (https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket : buckets.iterateAll()) {\n System.out.println(bucket.getName());\n }\n }\n }\n\n### Node.js\n\n\nFor more information, see the\n[Cloud Storage Node.js API\nreference documentation](https://cloud.google.com/nodejs/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 // Imports the Google Cloud client library\n const {Storage} = require('https://cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');\n\n // Creates a client\n const storage = new Storage();\n\n async function listBuckets() {\n const [buckets] = await storage.https://cloud.google.com/nodejs/docs/reference/storage/latest/storage/storage_2.html();\n\n console.log('Buckets:');\n buckets.forEach(bucket =\u003e {\n console.log(bucket.name);\n });\n }\n\n listBuckets().catch(console.error);\n\n### PHP\n\n\nFor more information, see the\n[Cloud Storage PHP API\nreference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).\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 use Google\\Cloud\\Storage\\StorageClient;\n\n /**\n * List all Cloud Storage buckets for the current project.\n */\n function list_buckets(): void\n {\n $storage = new StorageClient();\n foreach ($storage-\u003ebuckets() as $bucket) {\n printf('Bucket: %s' . PHP_EOL, $bucket-\u003ename());\n }\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 from google.cloud import https://cloud.google.com/python/docs/reference/storage/latest/\n\n\n def list_buckets():\n \"\"\"Lists all buckets.\"\"\"\n\n storage_client = https://cloud.google.com/python/docs/reference/storage/latest/.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()\n buckets = storage_client.https://cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_list_buckets()\n\n for bucket in buckets:\n print(bucket.name)\n\n### Ruby\n\n\nFor more information, see the\n[Cloud Storage Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).\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 def list_buckets\n require \"google/cloud/storage\"\n\n storage = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-storage-control-v2/latest/Google-Cloud-Storage.html.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html\n\n storage.buckets.https://cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Policy-Bindings.html do |bucket|\n puts bucket.name\n end\n end\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)."]]