使用物件訴訟保留

這個頁面說明如何使用物件訴訟保留,包括根據預設對新物件及個別物件提交訴訟保留。如需有關這個功能的詳細資訊,包括可對物件提交哪些類型的訴訟保留,請參閱「物件訴訟保留」一文。

必備條件

在 Cloud Storage 中使用這項功能之前,您應先具備以下條件:

  1. 具有足夠的權限可以查看和更新 Cloud Storage 中的值區及物件:

    • 如果您擁有一個包含值區的專案,代表您非常有可能已經具備必要權限。

    • 如果您使用身分與存取權管理 (IAM),應具有相關值區的 storage.buckets.updatestorage.buckets.getstorage.objects.updatestorage.objects.get 權限。請參閱使用 IAM 權限一文中的操作說明,瞭解如何取得具有這些權限的角色 (如 roles/storage.admin)。

    • 如果您使用 ACL,您應具備相關值區及其中物件的「擁有者」權限。相關操作說明請參閱「設定 ACL」一文。

使用依事件提交訴訟保留屬性的預設設定

下列工作示範如何啟用、查看和停用值區的依事件提交訴訟保留屬性的預設設定。啟用這個屬性時,系統會針對新增到值區的物件自動提交依據事件的訴訟保留。

啟用依事件提交訴訟保留屬性的預設設定

如要自動對每個新增到值區的新物件提交依據事件的訴訟保留,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找到您要啟用依事件提交訴訟保留屬性的值區,然後按一下該值區的名稱。

  3. 選取靠近頁面上方的 [Bucket Lock] (值區鎖定功能) 分頁標籤。

  4. 在「預設啟用依據事件的訴訟保留」項目中,按一下 [Disabled] (已停用) 按鈕。

gsutil

使用 gsutil retention event-default set 指令將 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil retention event-default set gs://[BUCKET_NAME]

程式碼範例

C++

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

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name) {
  StatusOr<gcs::BucketMetadata> original =
      client.GetBucketMetadata(bucket_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::BucketMetadata> patched_metadata = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetDefaultEventBasedHold(true),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!patched_metadata) {
    throw std::runtime_error(patched_metadata.status().message());
  }

  std::cout << "The default event-based hold for objects in bucket "
            << bucket_name << " is "
            << (patched_metadata->default_event_based_hold() ? "enabled"
                                                             : "disabled")
            << "\n";
}

C#

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

private void EnableBucketDefaultEventBasedHold(string bucketName)
{
    var storage = StorageClient.Create();
    var bucket = storage.GetBucket(bucketName);
    bucket.DefaultEventBasedHold = true;
    bucket = storage.UpdateBucket(bucket, new UpdateBucketOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = bucket.Metageneration
    });
    Console.WriteLine($"Default event-based hold was enabled for {bucketName}");
}

Go

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

bucket := c.Bucket(bucketName)
bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
	DefaultEventBasedHold: true,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
	return err
}

Java

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

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

Bucket bucket =
    storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build());

System.out.println("Default event-based hold was enabled for " + bucketName);

Node.js

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

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';

// Enables a default event-based hold for the bucket.
await storage.bucket(bucketName).setMetadata({
  defaultEventBasedHold: true,
});

console.log(`Default event-based hold was enabled for ${bucketName}.`);

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Enables a default event-based hold for a bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 */
function enable_default_event_based_hold($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $bucket->update(['defaultEventBasedHold' => true]);
    printf('Default event-based hold was enabled for %s' . PHP_EOL, $bucketName);
}

Python

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

# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)

bucket.default_event_based_hold = True
bucket.patch()

print('Default event based hold was enabled for {}'.format(bucket_name))

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"

require "google/cloud/storage"

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

bucket.update do |b|
  b.default_event_based_hold = true
end

puts "Default event-based hold was enabled for #{bucket_name}."

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 建立包含下列資訊的 .json 檔案:

    {
      "defaultEventBasedHold": true
    }
  3. 透過 PATCH Bucket 要求使用 cURL 呼叫 JSON API,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -H "Content-Type: application/json" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=defaultEventBasedHold"

XML API

XML API 無法使用物件訴訟保留,請改用 gsutil 等其他 Cloud Storage 工具。

取得值區的預設訴訟保留狀態

如要查看值區是否會預設對新物件提交依據事件的訴訟保留,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找到您要查看哪個值區的預設依據事件狀態,並按一下該值區名稱。

  3. 選取靠近頁面上方的 [Bucket Lock] (值區鎖定功能) 分頁標籤。

  4. 狀態會顯示在「預設啟用依據事件的訴訟保留」項目中。

gsutil

使用以下 gsutil ls 指令,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil ls -L -b gs://[BUCKET_NAME]/

回應類似下列範例:

gs://[BUCKET_NAME]/ :
  Storage class:         MULTI_REGIONAL
  ...
  ...
  Default Event-Based Hold: On
  ...

程式碼範例

C++

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

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name) {
  StatusOr<gcs::BucketMetadata> bucket_metadata =
      client.GetBucketMetadata(bucket_name);

  if (!bucket_metadata) {
    throw std::runtime_error(bucket_metadata.status().message());
  }

  std::cout << "The default event-based hold for objects in bucket "
            << bucket_metadata->name() << " is "
            << (bucket_metadata->default_event_based_hold() ? "enabled"
                                                            : "disabled")
            << "\n";
}

C#

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

private bool GetBucketDefaultEventBasedHold(string bucketName)
{
    var storage = StorageClient.Create();
    var bucket = storage.GetBucket(bucketName);
    bool? defaultEventBasedHoldOrNull = bucket?.DefaultEventBasedHold;
    bool defaultEventBasedHold =
        defaultEventBasedHoldOrNull.HasValue ? defaultEventBasedHoldOrNull.Value : false;
    Console.WriteLine("Default event-based hold: {0}", defaultEventBasedHold);
    return defaultEventBasedHold;
}

Go

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

attrs, err := c.Bucket(bucketName).Attrs(ctx)
if err != nil {
	return nil, err
}
log.Printf("Default event-based hold enabled? %t\n",
	attrs.DefaultEventBasedHold)

Java

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

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

Bucket bucket =
    storage.get(bucketName, BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));

if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) {
  System.out.println("Default event-based hold is enabled for " + bucketName);
} else {
  System.out.println("Default event-based hold is not enabled for " + bucketName);
}

Node.js

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

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';

// get bucketName metadata
const [metadata] = await storage.bucket(bucketName).getMetadata();
console.log(`Default event-based hold: ${metadata.defaultEventBasedHold}.`);

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Enables a default event-based hold for a bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 */
function get_default_event_based_hold($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);

    if ($bucket->info()['defaultEventBasedHold']) {
        printf('Default event-based hold is enabled for ' . $bucketName . PHP_EOL);
    } else {
        printf('Default event-based hold is not enabled for ' . $bucketName . PHP_EOL);
    }
}

Python

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

# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
bucket.reload()

if bucket.default_event_based_hold:
    print('Default event-based hold is enabled for {}'.format(bucket_name))
else:
    print('Default event-based hold is not enabled for {}'
          .format(bucket_name))

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"

require "google/cloud/storage"

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

if bucket.default_event_based_hold?
  puts "Default event-based hold is enabled for {bucket_name}."
else
  puts "Default event-based hold is not enabled for {bucket_name}."
end

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 透過內含所需 fieldsGET Bucket 要求,使用 cURL 呼叫 JSON API,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=defaultEventBasedHold"

    如果值區已啟用預設為依事件保留,回應會如下列範例所示:

    {
      "retentionPolicy": true
    }

XML API

XML API 無法使用物件訴訟保留,請改用 gsutil 等其他 Cloud Storage 工具。

停用依事件提交訴訟保留屬性的預設設定

如要停用對值區預設提交依據事件的訴訟保留,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找到您要停用依事件提交訴訟保留屬性的值區,然後按一下該值區的名稱。

  3. 選取靠近頁面上方的 [Bucket Lock] (值區鎖定功能) 分頁標籤。

  4. 在「預設啟用依據事件的訴訟保留」項目中,按一下 [Enabled] (已啟用) 按鈕。

gsutil

使用 gsutil retention event-default release 指令將 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil retention event-default release gs://[BUCKET_NAME]

程式碼範例

C++

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

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name) {
  StatusOr<gcs::BucketMetadata> original =
      client.GetBucketMetadata(bucket_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::BucketMetadata> patched_metadata = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetDefaultEventBasedHold(false),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!patched_metadata) {
    throw std::runtime_error(patched_metadata.status().message());
  }

  std::cout << "The default event-based hold for objects in bucket "
            << bucket_name << " is "
            << (patched_metadata->default_event_based_hold() ? "enabled"
                                                             : "disabled")
            << "\n";
}

C#

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

private void DisableBucketDefaultEventBasedHold(string bucketName)
{
    var storage = StorageClient.Create();
    var bucket = storage.GetBucket(bucketName);
    bucket.DefaultEventBasedHold = false;
    bucket = storage.UpdateBucket(bucket, new UpdateBucketOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = bucket.Metageneration
    });
    Console.WriteLine($"Default event-based hold was disabled for {bucketName}");
}

Go

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

bucket := c.Bucket(bucketName)
bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
	DefaultEventBasedHold: false,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
	return err
}

Java

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

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

Bucket bucket =
    storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(false).build());

System.out.println("Default event-based hold was disabled for " + bucketName);

Node.js

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

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';

// Disables a default event-based hold for a bucket.
await storage.bucket(bucketName).setMetadata({
  defaultEventBasedHold: false,
});
console.log(`Default event-based hold was disabled for ${bucketName}.`);

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Disables a default event-based hold for a bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 */
function disable_default_event_based_hold($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $bucket->update(['defaultEventBasedHold' => false]);
    printf('Default event-based hold was disabled for %s' . PHP_EOL, $bucketName);
}

Python

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

# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)

bucket.default_event_based_hold = False
bucket.patch()

print("Default event based hold was disabled for {}".format(bucket_name))

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"

require "google/cloud/storage"

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

bucket.update do |b|
  b.default_event_based_hold = false
end

puts "Default event-based hold was disabled for #{bucket_name}."

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 建立包含下列資訊的 .json 檔案:

    {
      "defaultEventBasedHold": false
    }
  3. 透過 PATCH Bucket 要求使用 cURL 呼叫 JSON API,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -H "Content-Type: application/json" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=defaultEventBasedHold"

XML API

XML API 無法使用物件訴訟保留,請改用 gsutil 等其他 Cloud Storage 工具。

使用物件訴訟保留

下列工作示範如何查看物件的訴訟保留,以及如何對物件提交和釋出訴訟保留

提交物件訴訟保留

如要對值區內的物件提交訴訟保留,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找到您要提交訴訟保留的物件所屬的值區,然後按一下該值區的名稱。

  3. 找到您要提交訴訟保留的物件,並按一下該物件名稱旁邊的核取方塊。

  4. 按一下 [Manage holds] (管理訴訟保留) 按鈕。

    「Manage holds」(管理訴訟保留) 視窗隨即出現。

  5. 找到您想要對物件提交哪種類型的訴訟保留,然後按一下對應的核取方塊。

  6. 按一下 [Save hold settings] (儲存訴訟保留設定)

gsutil

使用 gsutil retention 指令搭配 tempevent[HOLD_TYPE],並將其他 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil retention [HOLD_TYPE] set gs://[BUCKET_NAME]/[OBJECT_NAME]

程式碼範例

C++

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

下例示範如何對物件設定依據事件的訴訟保留:

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  StatusOr<gcs::ObjectMetadata> original =
      client.GetObjectMetadata(bucket_name, object_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::ObjectMetadata> updated = client.PatchObject(
      bucket_name, object_name,
      gcs::ObjectMetadataPatchBuilder().SetEventBasedHold(true),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!updated) {
    throw std::runtime_error(updated.status().message());
  }

  std::cout << "The event hold for object " << updated->name()
            << " in bucket " << updated->bucket() << " is "
            << (updated->event_based_hold() ? "enabled" : "disabled") << "\n";
}

下例示範如何對物件設定暫時性訴訟保留:

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  StatusOr<gcs::ObjectMetadata> original =
      client.GetObjectMetadata(bucket_name, object_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::ObjectMetadata> updated = client.PatchObject(
      bucket_name, object_name,
      gcs::ObjectMetadataPatchBuilder().SetTemporaryHold(true),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!updated) {
    throw std::runtime_error(updated.status().message());
  }

  std::cout << "The temporary hold for object " << updated->name()
            << " in bucket " << updated->bucket() << " is "
            << (updated->temporary_hold() ? "enabled" : "disabled") << "\n";
}

C#

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

下例示範如何對物件設定依據事件的訴訟保留:

private void SetObjectEventBasedHold(string bucketName,
    string objectName)
{
    var storage = StorageClient.Create();
    var storageObject = storage.GetObject(bucketName, objectName);
    storageObject.EventBasedHold = true;
    storageObject = storage.UpdateObject(storageObject, new UpdateObjectOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = storageObject.Metageneration
    });
}

下例示範如何對物件設定暫時性訴訟保留:

private void SetObjectTemporaryHold(string bucketName,
    string objectName)
{
    var storage = StorageClient.Create();
    var storageObject = storage.GetObject(bucketName, objectName);
    storageObject.TemporaryHold = true;
    storageObject = storage.UpdateObject(storageObject, new UpdateObjectOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = storageObject.Metageneration,
    });
}

Go

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

下例示範如何對物件設定依據事件的訴訟保留:

o := client.Bucket(bucket).Object(object)
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
	EventBasedHold: true,
}
if _, err := o.Update(ctx, objectAttrsToUpdate); err != nil {
	return err
}

下例示範如何對物件設定暫時性訴訟保留:

o := client.Bucket(bucket).Object(object)
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
	TemporaryHold: true,
}
if _, err := o.Update(ctx, objectAttrsToUpdate); err != nil {
	return err
}

Java

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

下例示範如何對物件設定依據事件的訴訟保留:

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

// The name of a blob, e.g. "my-blob"
// String blobName = "my-blob";

BlobId blobId = BlobId.of(bucketName, blobName);
Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build());

System.out.println("Event-based hold was set for " + blobName);

下例示範如何對物件設定暫時性訴訟保留:

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

// The name of a blob, e.g. "my-blob"
// String blobName = "my-blob";

BlobId blobId = BlobId.of(bucketName, blobName);
Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build());

System.out.println("Temporary hold was set for " + blobName);

Node.js

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

下例示範如何對物件設定依據事件的訴訟保留:

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to access, e.g. file.txt';

// Set event-based hold
await storage
  .bucket(bucketName)
  .file(fileName)
  .setMetadata({
    eventBasedHold: true,
  });
console.log(`Event-based hold was set for ${fileName}.`);

下例示範如何對物件設定暫時性訴訟保留:

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to access, e.g. file.txt';

await storage
  .bucket(bucketName)
  .file(fileName)
  .setMetadata({
    temporaryHold: true,
  });
console.log(`Temporary hold was set for ${fileName}.`);

PHP

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

下例示範如何對物件設定依據事件的訴訟保留:

use Google\Cloud\Storage\StorageClient;

/**
 * Sets an event-based hold for an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 */
function set_event_based_hold($bucketName, $objectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->update(['eventBasedHold' => true]);
    printf('Event-based hold was set for %s' . PHP_EOL, $objectName);
}

下例示範如何對物件設定暫時性訴訟保留:

use Google\Cloud\Storage\StorageClient;

/**
 * Sets a temporary hold for an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 */
function set_temporary_hold($bucketName, $objectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->update(['temporaryHold' => true]);
    printf('Temporary hold was set for %s' . PHP_EOL, $objectName);
}

Python

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

下例示範如何對物件設定依據事件的訴訟保留:

# bucket_name = "my-bucket"
# blob_name = "my-blob"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

blob.event_based_hold = True
blob.patch()

print('Event based hold was set for {}'.format(blob_name))

下例示範如何對物件設定暫時性訴訟保留:

# bucket_name = "my-bucket"
# blob_name = "my-blob"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

blob.temporary_hold = True
blob.patch()

print("Temporary hold was set for #{blob_name}")

Ruby

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

下例示範如何對物件設定依據事件的訴訟保留:

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of a file in the Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.set_event_based_hold!

puts "Event-based hold was set for #{file_name}."

下例示範如何對物件設定暫時性訴訟保留:

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of a file in the Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.set_temporary_hold!

puts "Temporary hold was set for #{file_name}."

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 建立一個包含下列資訊的 .json 檔案,並將 [HOLD_TYPE] 替換成 temporaryHoldeventBasedHold

    {
      "[HOLD_TYPE]": true
    }
  3. 透過 PATCH Object 要求使用 cURL 呼叫 JSON API,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -H "Content-Type: application/json" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]"

XML API

XML API 無法使用物件訴訟保留,請改用 gsutil 等其他 Cloud Storage 工具。

取得物件的訴訟保留狀態

如要查看物件目前有哪些訴訟保留,請參閱「查看物件中繼資料」說明文章中提供的一般指示說明。

釋出物件訴訟保留

如要釋出值區內物件的訴訟保留,請按照以下指示操作:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中,找到您要移除訴訟保留的物件所屬的值區,然後按一下該值區的名稱。

  3. 找到您要移除訴訟保留的物件,並按一下該物件名稱旁邊的核取方塊。

  4. 按一下 [Manage holds] (管理訴訟保留) 按鈕。

    「Manage holds」(管理訴訟保留) 視窗隨即出現。

  5. 找到您要移除的訴訟保留,並取消選取對應的核取方塊。

  6. 按一下 [Save hold settings] (儲存訴訟保留設定)

gsutil

使用 gsutil retention 指令搭配 tempevent[HOLD_TYPE],並將其他 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil retention [HOLD_TYPE] release gs://[BUCKET_NAME]/[OBJECT_NAME]

程式碼範例

C++

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

下例示範如何對物件設定依據事件的訴訟保留:

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  StatusOr<gcs::ObjectMetadata> original =
      client.GetObjectMetadata(bucket_name, object_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::ObjectMetadata> updated = client.PatchObject(
      bucket_name, object_name,
      gcs::ObjectMetadataPatchBuilder().SetEventBasedHold(false),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!updated) {
    throw std::runtime_error(updated.status().message());
  }

  std::cout << "The event hold for object " << updated->name()
            << " in bucket " << updated->bucket() << " is "
            << (updated->event_based_hold() ? "enabled" : "disabled") << "\n";
}

下例示範如何對物件設定暫時性訴訟保留:

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name) {
  StatusOr<gcs::ObjectMetadata> original =
      client.GetObjectMetadata(bucket_name, object_name);

  if (!original) {
    throw std::runtime_error(original.status().message());
  }

  StatusOr<gcs::ObjectMetadata> updated = client.PatchObject(
      bucket_name, object_name,
      gcs::ObjectMetadataPatchBuilder().SetTemporaryHold(false),
      gcs::IfMetagenerationMatch(original->metageneration()));

  if (!updated) {
    throw std::runtime_error(updated.status().message());
  }

  std::cout << "The temporary hold for object " << updated->name()
            << " in bucket " << updated->bucket() << " is "
            << (updated->temporary_hold() ? "enabled" : "disabled") << "\n";
}

C#

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

下例示範如何釋出物件的依據事件訴訟保留:

private void ReleaseObjectEventBasedHold(string bucketName,
    string objectName)
{
    var storage = StorageClient.Create();
    var storageObject = storage.GetObject(bucketName, objectName);
    storageObject.EventBasedHold = false;
    storageObject = storage.UpdateObject(storageObject, new UpdateObjectOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = storageObject.Metageneration,
    });
}

下例示範如何釋出物件的暫時性訴訟保留:

private void ReleaseObjectTemporaryHold(string bucketName,
    string objectName)
{
    var storage = StorageClient.Create();
    var storageObject = storage.GetObject(bucketName, objectName);
    storageObject.TemporaryHold = false;
    storageObject = storage.UpdateObject(storageObject, new UpdateObjectOptions()
    {
        // Use IfMetagenerationMatch to avoid race conditions.
        IfMetagenerationMatch = storageObject.Metageneration,
    });
}

Go

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

下例示範如何釋出物件的依據事件訴訟保留:

o := client.Bucket(bucket).Object(object)
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
	EventBasedHold: false,
}
if _, err := o.Update(ctx, objectAttrsToUpdate); err != nil {
	return err
}

下例示範如何釋出物件的暫時性訴訟保留:

o := client.Bucket(bucket).Object(object)
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
	TemporaryHold: false,
}
if _, err := o.Update(ctx, objectAttrsToUpdate); err != nil {
	return err
}

Java

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

下例示範如何釋出物件的依據事件訴訟保留:

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

// The name of a blob, e.g. "my-blob"
// String blobName = "my-blob";

BlobId blobId = BlobId.of(bucketName, blobName);
Blob blob = storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(false).build());

System.out.println("Event-based hold was released for " + blobName);

下例示範如何釋出物件的暫時性訴訟保留:

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

// The name of a blob, e.g. "my-blob"
// String blobName = "my-blob";

BlobId blobId = BlobId.of(bucketName, blobName);
Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build());

System.out.println("Temporary hold was released for " + blobName);

Node.js

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

下例示範如何釋出物件的依據事件訴訟保留:

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to access, e.g. file.txt';

await storage
  .bucket(bucketName)
  .file(fileName)
  .setMetadata({
    eventBasedHold: false,
  });
console.log(`Event-based hold was released for ${fileName}.`);

下例示範如何釋出物件的暫時性訴訟保留:

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

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'File to access, e.g. file.txt';

await storage
  .bucket(bucketName)
  .file(fileName)
  .setMetadata({
    temporaryHold: false,
  });
console.log(`Temporary hold was released for ${fileName}.`);

PHP

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

下例示範如何釋出物件的依據事件訴訟保留:

use Google\Cloud\Storage\StorageClient;

/**
 * Releases an event-based hold for an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 */
function release_event_based_hold($bucketName, $objectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->update(['eventBasedHold' => false]);
    printf('Event-based hold was released for %s' . PHP_EOL, $objectName);
}

下例示範如何釋出物件的暫時性訴訟保留:

use Google\Cloud\Storage\StorageClient;

/**
 * Releases a temporary hold for an object.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $objectName the name of your Cloud Storage object.
 */
function release_temporary_hold($bucketName, $objectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->update(['temporaryHold' => false]);
    printf('Temporary hold was released for %s' . PHP_EOL, $objectName);
}

Python

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

下例示範如何釋出物件的依據事件訴訟保留:

# bucket_name = "my-bucket"
# blob_name = "my-blob"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

blob.event_based_hold = False
blob.patch()

print('Event based hold was released for {}'.format(blob_name))

下例示範如何釋出物件的暫時性訴訟保留:

# bucket_name = "my-bucket"
# blob_name = "my-blob"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

blob.temporary_hold = False
blob.patch()

print("Temporary hold was release for #{blob_name}")

Ruby

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

下例示範如何釋出物件的依據事件訴訟保留:

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of a file in the Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.release_event_based_hold!

puts "Event-based hold was released for #{file_name}."

下例示範如何釋出物件的暫時性訴訟保留:

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"
# file_name   = "Name of a file in the Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name
file    = bucket.file file_name

file.release_temporary_hold!

puts "Temporary hold was released for #{file_name}."

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用您自己的 OAuth 憑證。
  2. 建立一個包含下列資訊的 .json 檔案,並將 [HOLD_TYPE] 替換成 temporaryHoldeventBasedHold

    {
      "[HOLD_TYPE]": false
    }
  3. 透過 PATCH Object 要求使用 cURL 呼叫 JSON API,並將 [VALUES_IN_BRACKETS] 替換為適當的值:

    curl -X PATCH --data-binary @[JSON_FILE_NAME].json \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -H "Content-Type: application/json" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]"

XML API

XML API 無法使用物件訴訟保留,請改用 gsutil 等其他 Cloud Storage 工具。

後續步驟

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Storage
需要協助嗎?請前往我們的支援網頁