變更值區的預設儲存空間級別

本頁說明如何變更值區的「預設儲存空間級別」。將物件上傳至值區時,如果沒有指定物件的儲存空間級別,系統會將值區的預設儲存空間級別指派給物件。如要進一步瞭解儲存空間級別,請參閱儲存空間級別一文。

必要的角色

如要取得變更值區儲存空間類別所需的權限,請要求管理員為您授予值區的「儲存空間管理員」(roles/storage.admin) IAM 角色。

這個預先定義的角色具備變更值區儲存空間類別所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

  • storage.buckets.get
    • 如果您打算使用Trusted Cloud 控制台執行本頁的操作說明,才需要這項權限。
  • storage.buckets.list
    • 如果您打算使用Trusted Cloud 控制台執行本頁的操作說明,才需要這項權限。
  • storage.buckets.update

您或許還可透過自訂角色取得這些權限。

如需如何授予值區角色的操作說明,請參閱「搭配值區使用 IAM」。

變更值區的預設儲存空間級別

控制台

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

    前往「Buckets」(值區) 頁面

  2. 在值區清單中,找出要變更儲存空間類別的值區,然後點選該值區的名稱。

  3. 在「Bucket details」(值區詳細資料) 頁面中,按一下「Configuration」(設定) 分頁標籤。

  4. 按一下「預設儲存空間級別」的「編輯」圖示 ()。

  5. 在重疊顯示的視窗中,選取要用於值區的新預設儲存空間級別。

  6. 按一下 [儲存]

如要瞭解如何透過 Trusted Cloud 控制台取得 Cloud Storage 作業失敗的詳細錯誤資訊,請參閱「疑難排解」一文。

指令列

使用加上 --default-storage-class 旗標的 gcloud storage buckets update 指令:

gcloud storage buckets update gs://BUCKET_NAME --default-storage-class=STORAGE_CLASS

其中:

  • BUCKET_NAME 是相關值區的名稱。例如:my-bucket
  • STORAGE_CLASS 是您要為值區提供的新儲存空間級別。例如:nearline

回應類似下列範例:

Setting default storage class to "nearline" for bucket gs://my-bucket

用戶端程式庫

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, std::string const& bucket_name,
   std::string const& storage_class) {
  StatusOr<gcs::BucketMetadata> original =
      client.GetBucketMetadata(bucket_name);
  if (!original) throw std::move(original).status();

  gcs::BucketMetadata desired = *original;
  desired.set_storage_class(storage_class);

  StatusOr<gcs::BucketMetadata> patched =
      client.PatchBucket(bucket_name, *original, desired);
  if (!patched) throw std::move(patched).status();

  std::cout << "Storage class for bucket " << patched->name()
            << " has been patched to " << patched->storage_class() << "."
            << "\nFull metadata: " << *patched << "\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;

public class ChangeDefaultStorageClassSample
{
	public Bucket ChangeDefaultStorageClass(string bucketName = "your-bucket-name", string storageClass = StorageClasses.Standard)
	{
	    var storage = StorageClient.Create();
	    var bucket = storage.GetBucket(bucketName);

	    bucket.StorageClass = storageClass;

	    bucket = storage.UpdateBucket(bucket);
	    Console.WriteLine($"Default storage class for bucket {bucketName} changed to {storageClass}.");
	    return bucket;
	}
}

Go

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

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

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

import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/storage"
)

// changeDefaultStorageClass changes the storage class on a bucket.
func changeDefaultStorageClass(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %w", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
	defer cancel()

	bucket := client.Bucket(bucketName)
	newStorageClass := "COLDLINE"
	bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
		StorageClass: newStorageClass,
	}
	if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
	}
	fmt.Fprintf(w, "Default storage class for bucket %v has been set to %v\n", bucketName, newStorageClass)
	return nil
}

Java

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

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

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

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;

public class ChangeDefaultStorageClass {
  public static void changeDefaultStorageClass(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    // See the StorageClass documentation for other valid storage classes:
    // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
    StorageClass storageClass = StorageClass.COLDLINE;

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    bucket = bucket.toBuilder().setStorageClass(storageClass).build().update();

    System.out.println(
        "Default storage class for bucket "
            + bucketName
            + " has been set to "
            + bucket.getStorageClass());
  }
}

Node.js

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

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The name of a storage class
// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
// const storageClass = 'coldline';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function changeDefaultStorageClass() {
  await storage.bucket(bucketName).setStorageClass(storageClass);

  console.log(`${bucketName} has been set to ${storageClass}`);
}

changeDefaultStorageClass().catch(console.error);

PHP

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

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

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

use Google\Cloud\Storage\StorageClient;

/**
 * Change the default storage class for the given bucket.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 */
function change_default_storage_class(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);

    $storageClass = 'COLDLINE';

    $bucket->update([
        'storageClass' => $storageClass,
    ]);

    printf(
        'Default storage class for bucket %s has been set to %s',
        $bucketName,
        $storageClass
    );
}

Python

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

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

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

from google.cloud import storage
from google.cloud.storage import constants


def change_default_storage_class(bucket_name):
    """Change the default storage class of the bucket"""
    # bucket_name = "your-bucket-name"

    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    bucket.storage_class = constants.COLDLINE_STORAGE_CLASS
    bucket.patch()

    print(f"Default storage class for bucket {bucket_name} has been set to {bucket.storage_class}")
    return bucket

Ruby

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

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

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

def change_default_storage_class bucket_name:
  # The ID of your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket = storage.bucket bucket_name

  bucket.storage_class = "COLDLINE"

  puts "Default storage class for bucket #{bucket_name} has been set to #{bucket.storage_class}"
end

REST API

JSON API

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

  2. 建立包含下列資訊的 JSON 檔案:

    {
      "storageClass": "STORAGE_CLASS"
    }

    其中 STORAGE_CLASS 是您要為值區提供的新 儲存空間級別。例如:nearline

  3. 使用 cURL 來透過 PATCH 值區要求呼叫 JSON API

    curl -X PATCH --data-binary @JSON_FILE_NAME \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://storage.s3nsapis.fr/storage/v1/b/BUCKET_NAME?fields=storageClass"

    其中:

    • JSON_FILE_NAME 是您在步驟 2 建立的 JSON 檔案路徑。
    • BUCKET_NAME 是相關值區的名稱。例如:my-bucket

XML API

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

  2. 建立包含下列資訊的 XML 檔案:

    <StorageClass>STORAGE_CLASS</StorageClass>

    其中 STORAGE_CLASS 是您要為值區提供的新 儲存空間級別名稱。例如:nearline

  3. 使用 cURL 透過 ?storageClass 範圍內的 PUT 值區要求呼叫 XML API

    curl -X PUT --data-binary @XML_FILE_NAME \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      "https://storage.s3nsapis.fr/BUCKET_NAME?storageClass"

    其中:

    • XML_FILE_NAME 是您在步驟 2 建立的 XML 檔案路徑。
    • BUCKET_NAME 是相關值區的名稱。例如:my-bucket

後續步驟