使用存储分区标签

本页面介绍如何添加、查看、修改和移除在 Cloud Storage 存储分区上设置的标签。如需简要了解存储分区和存储分区标签,请参阅关键词汇

添加存储分区标签

Console

  1. 在 Google Cloud Platform Console 中打开 Cloud Storage 浏览器。
    打开 Cloud Storage 浏览器
  2. 在存储分区列表中,找到要应用标签的存储分区,然后点击其更多选项按钮(三个垂直排列的点)。

  3. 点击修改标签

  4. 在显示的侧边面板中,点击 + 添加标签按钮。

  5. 为您的标签指定 keyvalue

  6. 点击保存

修改标签侧边面板中,您还可以修改现有标签的

gsutil

label ch 命令中使用 -l 标志。例如:

gsutil label ch -l [KEY_1]:[VALUE_1] gs://[BUCKET_NAME]/

其中,[KEY_1] 是标签的键名,[VALUE_1] 是标签的值,[BUCKET_NAME] 则是该标签应用于的存储分区的名称。

  • 可在单个命令中使用多个 -l flags to add or edit multiple key:value pairs in a single command.

  • 使用 label set 命令将所有现有标签替换为新标签。

代码示例

C++

如需了解更多信息,请参阅 Cloud Storage C++ API 参考文档

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string label_key,
   std::string label_value) {
  StatusOr<gcs::BucketMetadata> updated_metadata = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetLabel(label_key, label_value));

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

  std::cout << "Successfully set label " << label_key << " to " << label_value
            << " on bucket  " << updated_metadata->name() << ".";
  std::cout << " The bucket labels are now:";
  for (auto const& kv : updated_metadata->labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << "\n";
}

PHP

如需了解详情,请参阅 Cloud Storage PHP API 参考文档

use Google\Cloud\Storage\StorageClient;

/**
 * Adds or updates a bucket label.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $labelName the name of the label to add.
 * @param string $labelValue the value of the label to add.
 */
function add_bucket_label($bucketName, $labelName, $labelValue)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $newLabels = [$labelName => $labelValue];
    $bucket->update(['labels' => $newLabels]);
    printf('Added label %s (%s) to %s' . PHP_EOL, $labelName, $labelValue, $bucketName);
}

Python

如需了解详情,请参阅 Cloud Storage Python API 参考文档

def add_bucket_label(bucket_name):
    """Add a label to a bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    labels = bucket.labels
    labels['example'] = 'label'
    bucket.labels = labels
    bucket.patch()

    print('Updated labels on {}.'.format(bucket.name))
    pprint.pprint(bucket.labels)

Ruby

如需了解详情,请参阅 Cloud Storage Ruby API 参考文档

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Name of your Google Cloud Storage bucket"
# label_key   = "Cloud Storage bucket Label Key"
# label_value = "Cloud Storage bucket Label Value"

require "google/cloud/storage"

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

bucket.update do |bucket|
  bucket.labels[label_key] = label_value
end

puts "Added label #{label_key} with value #{label_value} to #{bucket_name}"

REST API

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 创建一个包含以下信息的 .json 文件,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
  3. {
      "labels": {
        "[KEY_1]": "[VALUE_1]"
      }
    }
  4. 使用 cURL(附带 PATCH 存储分区请求)调用 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=labels"

您可以在请求中添加或修改多个 key:value 对。

XML API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 创建一个包含以下信息的 .xml 文件,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    <Tagging>
      <TagSet>
        <Tag>
          <Key>[KEY_1]</Key>
          <Value>[VALUE_1]</Value>
        </Tag>
      </TagSet>
    </Tagging>

    请注意,您可以在请求中添加多个 <Tag> 元素。

  3. 使用 cURL(附带 PUT 存储分区请求和 tagging 查询字符串参数)调用 XML API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

查看存储分区标签

Console

  1. 在 Google Cloud Platform Console 中打开 Cloud Storage 浏览器。
    打开 Cloud Storage 浏览器
  2. 在存储分区列表中,找到要验证的存储分区,然后检查其标签值。

    验证“Multi-Regional 存储空间”存储分区类别。

gsutil

使用以下 gsutil ls 命令,并将 [VALUES_IN_BRACKETS] 替换为适当的值:

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

响应类似如下示例:

gs://[BUCKET_NAME]/ :
  Storage class:         MULTI_REGIONAL
  ...
  ...
  Labels:
    {
      "your_label_key": "your_label_value"
    }
  ...

代码示例

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, gcs::Fields("labels"));

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

  if (bucket_metadata->labels().empty()) {
    std::cout << "The bucket " << bucket_name << " has no labels set.\n";
    return;
  }

  std::cout << "The labels for bucket " << bucket_name << " are:";
  for (auto const& kv : bucket_metadata->labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << "\n";
}

PHP

如需了解详情,请参阅 Cloud Storage PHP API 参考文档

use Google\Cloud\Storage\StorageClient;

/**
 * Prints a list of a bucket's lables.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 */
function get_bucket_labels($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $info = $bucket->info();
    if (isset($info['labels'])) {
        foreach ($info['labels'] as $labelKey => $labelValue) {
            printf('%s: %s' . PHP_EOL, $labelKey, $labelValue);
        }
    }
}

Python

如需了解详情,请参阅 Cloud Storage Python API 参考文档

def get_bucket_labels(bucket_name):
    """Prints out a bucket's labels."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    labels = bucket.labels
    pprint.pprint(labels)

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

puts "Labels for #{bucket_name}"
bucket.labels.each do |key, value|
  puts "#{key} = #{value}"
end

REST API

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 使用 cURL(附带包含所需 fieldsGET 存储分区请求)调用 JSON API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=labels"

响应类似如下示例:

{
  "labels" : {
     ([YOUR_LABEL_KEY]): [YOUR_LABEL_VALUE]
   },
}

XML API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 使用 cURL(附带包含 tagging 查询参数的 GET 存储分区请求)调用 XML API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    curl -X GET -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

响应类似如下示例:

<Tagging>
  <TagSet>
    <Tag>
      <Key>[YOUR_LABEL_KEY]</Key>
      <Value>[YOUR_LABEL_VALUE]</Value>
    </Tag>
  </TagSet>
</Tagging>

移除存储分区标签

Console

  1. 在 Google Cloud Platform Console 中打开 Cloud Storage 浏览器。
    打开 Cloud Storage 浏览器
  2. 在存储分区列表中,找到要移除其标签的存储分区,然后点击存储分区的更多选项按钮(三个垂直排列的点)。

  3. 点击修改标签

  4. 点击与要移除的标签关联的 X

  5. 点击保存

gsutil

label ch 命令中使用 -d 标志。例如:

gsutil label ch -d [KEY_1] gs://[BUCKET_NAME]/

其中,[KEY_1] 是标签的键名,[BUCKET_NAME] 是该标签应用于的存储分区的名称。

  • 使用 label set 命令将所有现有标签替换为新标签。

代码示例

C++

如需了解更多信息,请参阅 Cloud Storage C++ API 参考文档

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string label_key) {
  StatusOr<gcs::BucketMetadata> updated_metadata = client.PatchBucket(
      bucket_name, gcs::BucketMetadataPatchBuilder().ResetLabel(label_key));

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

  std::cout << "Successfully reset label " << label_key << " on bucket  "
            << updated_metadata->name() << ".";
  if (updated_metadata->labels().empty()) {
    std::cout << " The bucket now has no labels.\n";
    return;
  }
  std::cout << " The bucket labels are now:";
  for (auto const& kv : updated_metadata->labels()) {
    std::cout << "\n  " << kv.first << ": " << kv.second;
  }
  std::cout << "\n";
}

PHP

如需了解详情,请参阅 Cloud Storage PHP API 参考文档

use Google\Cloud\Storage\StorageClient;

/**
 * Removes a label from a bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 * @param string $labelName the name of the label to remove.
 */
function remove_bucket_label($bucketName, $labelName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $labels = [$labelName => null];
    $bucket->update(['labels' => $labels]);
    printf('Removed label %s from %s' . PHP_EOL, $labelName, $bucketName);
}

Python

如需了解详情,请参阅 Cloud Storage Python API 参考文档

def remove_bucket_label(bucket_name):
    """Remove a label from a bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)

    labels = bucket.labels

    if 'example' in labels:
        del labels['example']

    bucket.labels = labels
    bucket.patch()

    print('Updated labels on {}.'.format(bucket.name))
    pprint.pprint(bucket.labels)

Ruby

如需了解详情,请参阅 Cloud Storage Ruby API 参考文档

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

require "google/cloud/storage"

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

bucket.update do |bucket|
  bucket.labels[label_key] = nil
end

puts "Deleted label #{label_key} from #{bucket_name}"

REST API

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 创建一个包含以下信息的 .json 文件,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
  3. {
      "labels": {
        "[KEY_1]": null
      }
    }
  4. 使用 cURL(附带 PATCH 存储分区请求)调用 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=labels"

您可以移除请求中的多个 key:value 对。

XML API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 创建一个包含以下信息的 .xml 文件,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    <Tagging>
      <TagSet>
        <Tag>
        </Tag>
      </TagSet>
    </Tagging>
  3. 使用 cURL(附带 PUT 存储分区请求和 tagging 查询字符串参数)调用 XML API,并将 [VALUES_IN_BRACKETS] 替换为适当的值:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
        -H "Authorization: Bearer [OAUTH2_TOKEN]" \
        "https://storage.googleapis.com/[BUCKET_NAME]?tagging"

后续步骤

此页内容是否有用?请给出您的反馈和评价:

发送以下问题的反馈:

此网页
Cloud Storage
需要帮助?请访问我们的支持页面