如何更改Google Cloud Storage中存在的对象的所有特定文件的元数据?

问题描述 投票:0回答:2

我已将数千个文件上传到Google存储,但我发现所有文件都缺少内容类型,因此我的网站无法正确显示。

[我想知道是否可以设置某种策略,例如同时更改所有文件的内容类型,例如,我在存储桶中有一堆.html文件

a/b/index.html
a/c/a.html
a/c/a/b.html
a/a.html
.
.
.

是否可以使用一个命令在不同位置设置所有.html文件的内容类型?

google-cloud-storage mime-types content-type
2个回答
0
投票

[没有唯一的命令可以实现您要寻找的行为(一个命令可以编辑对象的所有元数据),但是,gcloud有一个命令可以编辑元数据,您可以在bash脚本上使用该命令来遍历所有对象桶中的物体。

1.-选项(1)是在bash脚本上使用gcloud命令“ setmeta”:

# kinda pseudo code here.

# get the list with all your object's names and iterate over the metadata edition command.

for OUTPUT in $(get_list_of_objects_names)
do

gsutil setmeta -h "[METADATA_KEY]:[METADATA_VALUE]" gs://[BUCKET_NAME]/[OBJECT_NAME]
# the "gs://[BUCKET_NAME]/[OBJECT_NAME]" would be your object name.

done

2.-您也可以创建C ++脚本来实现相同的目的:

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name,
   std::string key, std::string value) {

# you would need to find list all the objects, while on the loop, you can edit the metadata of the object.

for (auto&& object_metadata : client.ListObjects(bucket_name)) {

  string bucket_name=object_metadata->bucket(), object_name=object_metadata->name();

  StatusOr<gcs::ObjectMetadata> object_metadata =
      client.GetObjectMetadata(bucket_name, object_name);

  gcs::ObjectMetadata desired = *object_metadata;
  desired.mutable_metadata().emplace(key, value);

  StatusOr<gcs::ObjectMetadata> updated =
      client.UpdateObject(bucket_name, object_name, desired,
                          gcs::Generation(object_metadata->generation()))
}

}

0
投票

您可以做:

gsutil -m setmeta -h Content-Type:text/html gs://your-bucket/**.html
© www.soinside.com 2019 - 2024. All rights reserved.