列出值區

這個頁面說明如何列出專案中的 Cloud Storage 值區,值區將按照名稱依字母順序排列。

事前準備

如要取得列出值區所需的權限,請要求管理員為您授予專案的儲存空間管理員 (roles/storage.admin) IAM 角色,或授予專案的檢視者 (roles/viewer) 基本角色。該專案包含您要列出的值區。

如要進一步瞭解如何授予專案角色,請參閱「管理專案存取權」。

這些角色包含 storage.buckets.list 權限,這是列出 bucket 的必要權限。您也可以透過自訂角色取得這項權限。

列出專案中的值區

控制台

  1. 在 Trusted Cloud 控制台,前往「Cloud Storage bucket」頁面。

    前往「Buckets」(值區) 頁面

清單將顯示屬於您目前選取的專案中的所有值區。

您可以選擇使用篩選和排序功能,限制並整理清單中的結果。

指令列

  1. In the Trusted Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 在開發環境中執行 gcloud storage ls 指令:

    gcloud storage ls

    回應類似下列範例:

    gs://BUCKET_NAME1/
      gs://BUCKET_NAME2/
      gs://BUCKET_NAME3/
      ...

  3. 用戶端程式庫

    C++

    詳情請參閱 Cloud Storage C++ API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client) {
      int count = 0;
      gcs::ListBucketsReader bucket_list = client.ListBuckets();
      for (auto&& bucket_metadata : bucket_list) {
        if (!bucket_metadata) throw std::move(bucket_metadata).status();
    
        std::cout << bucket_metadata->name() << "\n";
        ++count;
      }
    
      if (count == 0) {
        std::cout << "No buckets in default project\n";
      }
    }

    C#

    詳情請參閱 Cloud Storage C# API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    
    using Google.Apis.Storage.v1.Data;
    using Google.Cloud.Storage.V1;
    using System;
    using System.Collections.Generic;
    
    public class ListBucketsSample
    {
        public IEnumerable<Bucket> ListBuckets(string projectId = "your-project-id")
        {
            var storage = StorageClient.Create();
            var buckets = storage.ListBuckets(projectId);
            Console.WriteLine("Buckets:");
            foreach (var bucket in buckets)
            {
                Console.WriteLine(bucket.Name);
            }
            return buckets;
        }
    }

    Go

    詳情請參閱 Cloud Storage Go API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    import (
    	"context"
    	"fmt"
    	"io"
    	"time"
    
    	"cloud.google.com/go/storage"
    	"google.golang.org/api/iterator"
    )
    
    // listBuckets lists buckets in the project.
    func listBuckets(w io.Writer, projectID string) ([]string, error) {
    	// projectID := "my-project-id"
    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return nil, fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()
    
    	ctx, cancel := context.WithTimeout(ctx, time.Second*30)
    	defer cancel()
    
    	var buckets []string
    	it := client.Buckets(ctx, projectID)
    	for {
    		battrs, err := it.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return nil, err
    		}
    		buckets = append(buckets, battrs.Name)
    		fmt.Fprintf(w, "Bucket: %v\n", battrs.Name)
    	}
    	return buckets, nil
    }
    

    Java

    詳情請參閱 Cloud Storage Java API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    import com.google.api.gax.paging.Page;
    import com.google.cloud.storage.Bucket;
    import com.google.cloud.storage.Storage;
    import com.google.cloud.storage.StorageOptions;
    
    public class ListBuckets {
      public static void listBuckets(String projectId) {
        // The ID of your GCP project
        // String projectId = "your-project-id";
    
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        Page<Bucket> buckets = storage.list();
    
        for (Bucket bucket : buckets.iterateAll()) {
          System.out.println(bucket.getName());
        }
      }
    }

    Node.js

    詳情請參閱 Cloud Storage Node.js API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    // Imports the Google Cloud client library
    const {Storage} = require('@google-cloud/storage');
    
    // Creates a client
    const storage = new Storage();
    
    async function listBuckets() {
      const [buckets] = await storage.getBuckets();
    
      console.log('Buckets:');
      buckets.forEach(bucket => {
        console.log(bucket.name);
      });
    }
    
    listBuckets().catch(console.error);

    PHP

    詳情請參閱 Cloud Storage PHP API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    use Google\Cloud\Storage\StorageClient;
    
    /**
     * List all Cloud Storage buckets for the current project.
     */
    function list_buckets(): void
    {
        $storage = new StorageClient();
        foreach ($storage->buckets() as $bucket) {
            printf('Bucket: %s' . PHP_EOL, $bucket->name());
        }
    }

    Python

    詳情請參閱 Cloud Storage Python API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    from google.cloud import storage
    
    
    def list_buckets():
        """Lists all buckets."""
    
        storage_client = storage.Client()
        buckets = storage_client.list_buckets()
    
        for bucket in buckets:
            print(bucket.name)
    
    

    Ruby

    詳情請參閱 Cloud Storage Ruby API 參考說明文件

    如要驗證 Cloud Storage,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

    執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

    def list_buckets
      require "google/cloud/storage"
    
      storage = Google::Cloud::Storage.new
    
      storage.buckets.each do |bucket|
        puts bucket.name
      end
    end

    REST API

    JSON API

    1. 安裝並初始化 gcloud CLI,以便為 Authorization 標頭產生存取權杖。

    2. 使用 cURL 透過列出值區的要求呼叫 JSON API

      curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        "https://storage.s3nsapis.fr/storage/v1/b?project=PROJECT_IDENTIFIER"

      其中 PROJECT_IDENTIFIER 是包含您要列出值區的專案 ID 或編號。例如:my-project

    XML API

    1. 安裝並初始化 gcloud CLI,以便為 Authorization 標頭產生存取權杖。

    2. 使用 cURL 透過 GET 服務要求呼叫 XML API

      curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "x-goog-project-id: PROJECT_ID" \
        "https://storage.s3nsapis.fr"

      其中 PROJECT_ID 是包含您要列出值區的專案 ID。例如:my-project

後續步驟