您可以通过 listObjectVersions 列举桶内的多版本对象信息,每次最大返回 1000 个对象。
tos:ListBucketVersions
权限,具体操作请参见 IAM 策略概述。参数 | 描述 |
---|---|
prefix | 本次查询结果的前缀。 |
delimiter | 对对象名称进行分组的字符。 |
maxKeys | 返回列举对象的最大数。默认为 1000。 |
encodingType | 对返回的内容进行编码并指定编码的类型。 |
keyMarker | 列举多版本对象的起始位置。设定从该值之后按字母排序返回对象列表。通常为上次请求返回的 nextMarker 值。 |
versionIdMarker | 与 keyMarker 配合使用,设定从该值之后按字典排序返回多版本对象列表。从上次列举结果中 nextVersionIdMarker 获取。 |
以下代码用于列举桶 examplebucket
中最多 10 个多版本对象。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); ListObjectVersionsV2Input input(bucketName); input.setMaxKeys(10); auto output = client.listObjectVersions(input); if (!output.isSuccess()) { // 异常处理 std::cout << "ListObjectVersions failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } std::cout << "ListObjectVersions success." << std::endl; for (const auto& versions : output.result().getVersions()) { std::cout << "versions"<< ",name:" << versions.getKey() << ",size:" << versions.getSize() << ",etag:" << versions.getETag() << ",is latest:" << versions.isLatest() << ",version id:" << versions.getVersionId() << ",hashCrc64:" << versions.getHashCrc64Ecma() << ",lastmodify time:" << versions.getStringFormatLastModified() << ",storage class:" << versions.getStringFormatStorageClass() << std:: endl; } for (const auto& deleteMarker : output.result().getDeleteMarkers()) { std::cout << "deleteMarkers"<< ",name:" << deleteMarker.getKey() << ",is latest:" << deleteMarker.isLatest() << ",version id:" << deleteMarker.getVersionId() << ",lastmodify time:" << deleteMarker.getStringFormatLastModified() << std:: endl; } // 释放网络等资源 CloseClient(); return 0; }
以下代码用于列举桶 examplebucket
前缀 yourkeyPrefix
下的所有多版本对象和删除标记。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 填写列举的文件前缀(Prefix)。 std::string prefix = "yourkeyPrefix"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); ListObjectVersionsV2Input input(bucketName); input.setMaxKeys(1000); input.setPrefix(prefix); bool isTruncated = true; std::string nextKeyMarker = ""; std::string nextVersionIdMarker = ""; while(isTruncated){ input.setKeyMarker(nextKeyMarker); auto output = client.listObjectVersions(input); if (!output.isSuccess()) { // 异常处理 std::cout << "ListObjectVersions failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } nextKeyMarker = output.result().getKeyMarker(); isTruncated = output.result().isTruncated(); std::cout << "ListObjectVersions success." << std::endl; for (const auto& versions : output.result().getVersions()) { std::cout << "versions"<< ",name:" << versions.getKey() << ",size:" << versions.getSize() << ",etag:" << versions.getETag() << ",is latest:" << versions.isLatest() << ",version id:" << versions.getVersionId() << ",hashCrc64:" << versions.getHashCrc64Ecma() << ",lastmodify time:" << versions.getStringFormatLastModified() << ",storage class:" << versions.getStringFormatStorageClass() << std:: endl; } for (const auto& deleteMarker : output.result().getDeleteMarkers()) { std::cout << "deleteMarkers"<< ",name:" << deleteMarker.getKey() << ",is latest:" << deleteMarker.isLatest() << ",version id:" << deleteMarker.getVersionId() << ",lastmodify time:" << deleteMarker.getStringFormatLastModified() << std:: endl; } } // 释放网络等资源 CloseClient(); return 0; }
以下代码用于列举桶 examplebucket
下的所有多版本对象和删除标记。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); ListObjectVersionsV2Input input(bucketName); input.setMaxKeys(1000); bool isTruncated = true; std::string nextKeyMarker = ""; std::string nextVersionIdMarker = ""; while(isTruncated){ input.setKeyMarker(nextKeyMarker); input.setVersionIdMarker(nextVersionIdMarker); auto output = client.listObjectVersions(input); if (!output.isSuccess()) { // 异常处理 std::cout << "ListObjectVersions failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } nextKeyMarker = output.result().getNextKeyMarker(); nextVersionIdMarker = output.result().getNextVersionIdMarker(); isTruncated = output.result().isTruncated(); std::cout << "ListObjectVersions success." << std::endl; for (const auto& versions : output.result().getVersions()) { std::cout << "versions"<< ",name:" << versions.getKey() << ",size:" << versions.getSize() << ",etag:" << versions.getETag() << ",is latest:" << versions.isLatest() << ",version id:" << versions.getVersionId() << ",hashCrc64:" << versions.getHashCrc64Ecma() << ",lastmodify time:" << versions.getStringFormatLastModified() << ",storage class:" << versions.getStringFormatStorageClass() << std:: endl; } for (const auto& deleteMarker : output.result().getDeleteMarkers()) { std::cout << "deleteMarkers"<< ",name:" << deleteMarker.getKey() << ",is latest:" << deleteMarker.isLatest() << ",version id:" << deleteMarker.getVersionId() << ",lastmodify time:" << deleteMarker.getStringFormatLastModified() << std:: endl; } } // 释放网络等资源 CloseClient(); return 0; }
TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 /
结尾的对象, 模拟目录的功能。
通过 delimiter
和 prefix
两个参数可以模拟目录的功能:
delimiter
为 /
同时设置 prefix
为空, 可返回根目录下的对象和子目录信息。delimiter
为 /
同时设置 prefix
为子目录(subfiledir
), 可返回子目录的对象和次级目录。以下代码用于递归列举目录下的对象和子目录,对象信息所有版本的,目录信息只返回最新版本。
#include "TosClientV2.h" using namespace VolcengineTos; int main(void){ // 初始化 TOS 账号信息 // Your Region 填写 Bucket 所在 Region std::string region = "Your Region"; std::string accessKey = std::getenv("TOS_ACCESS_KEY"); std::string secretKey = std::getenv("TOS_SECRET_KEY"); // 填写 Bucket 名称,例如 examplebucket std::string bucketName = "examplebucket"; // 填写列举的文件前缀(Prefix)。 std::string prefix = "yourkeyPrefix"; // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); ListObjectVersionsV2Input input(bucketName); input.setDelimiter("/"); input.setMaxKeys(100); bool isTruncated = true; std::string nextKeyMarker = ""; std::string nextVersionIdMarker = ""; while(isTruncated){ input.setKeyMarker(nextKeyMarker); input.setVersionIdMarker(nextVersionIdMarker); auto output = client.listObjectVersions(input); if (!output.isSuccess()) { // 异常处理 std::cout << "ListObjectVersions failed." << output.error().String() << std::endl; // 释放网络等资源 CloseClient(); return -1; } nextKeyMarker = output.result().getNextKeyMarker(); nextVersionIdMarker = output.result().getNextVersionIdMarker(); isTruncated = output.result().isTruncated(); std::cout << "ListObjectVersions success." << std::endl; for (const auto& versions : output.result().getVersions()) { std::cout << "versions"<< ",name:" << versions.getKey() << ",size:" << versions.getSize() << ",etag:" << versions.getETag() << ",is latest:" << versions.isLatest() << ",version id:" << versions.getVersionId() << ",hashCrc64:" << versions.getHashCrc64Ecma() << ",lastmodify time:" << versions.getStringFormatLastModified() << ",storage class:" << versions.getStringFormatStorageClass() << std::endl; } for (const auto& deleteMarker : output.result().getDeleteMarkers()) { std::cout << "deleteMarkers"<< ",name:" << deleteMarker.getKey() << ",is latest:" << deleteMarker.isLatest() << ",version id:" << deleteMarker.getVersionId() << ",lastmodify time:" << deleteMarker.getStringFormatLastModified() << std::endl; } for (const auto& prefix : output.result().getCommonPrefixes()) { std::cout << "Common Prefixes"<< prefix.getPrefix() << std::endl; } } // 释放网络等资源 CloseClient(); return 0; }
关于列举多版本对象的 API 文档,请参见 ListObjectsVersions。