C++存儲空間清單
本文介紹如何添加、查看、批量列舉和刪除存儲空間(Bucket)的清單(Inventory)配置。
注意事項(xiàng)
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產(chǎn)品訪問OSS,請使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對應(yīng)關(guān)系,請參見OSS地域和訪問域名。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OssClient。
請確保您擁有調(diào)用添加、查看、列舉和刪除存儲空間清單配置的權(quán)限。Bucket所有者默認(rèn)擁有此類權(quán)限,如果您無此類權(quán)限,請先向Bucket所有者申請對應(yīng)操作的權(quán)限。
單個Bucket最多只能有1000條清單配置。
配置清單的源Bucket與存放導(dǎo)出的清單文件所在的目標(biāo)Bucket必須位于同一個Region。
添加清單配置
以下代碼用于為某個Bucket添加清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網(wǎng)絡(luò)等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
InventoryConfiguration inventoryConf;
/* 指定清單規(guī)則名稱,該名稱在當(dāng)前Bucket下必須全局唯一。*/
inventoryConf.setId("inventoryId");
/* 清單配置是否啟用的標(biāo)識,可選值為true或false。*/
inventoryConf.setIsEnabled(true);
/* (可選)清單篩選的前綴。指定前綴后,清單將篩選出符合前綴的Object。*/
inventoryConf.setFilter(InventoryFilter("objectPrefix"));
InventoryOSSBucketDestination dest;
/* 導(dǎo)出清單文件的文件格式。*/
dest.setFormat(InventoryFormat::CSV);
/* 存儲空間擁有者的賬戶UID。*/
dest.setAccountId("10988548********");
/* 指定角色名稱,該角色需要擁有讀取源存儲空間所有文件以及向目標(biāo)存儲空間寫入文件的權(quán)限,格式為acs:ram::uid:role/rolename。*/
dest.setRoleArn("acs:ram::10988548********:role/inventory-test");
/* 存放導(dǎo)出的清單文件的存儲空間。*/
dest.setBucket("yourDstBucketName");
/* 清單文件的存儲路徑前綴。*/
dest.setPrefix("yourPrefix");
/* (可選)清單文件的加密方式, 可選SSEOSS或者SSEKMS方式加密。*/
//dest.setEncryption(InventoryEncryption(InventorySSEOSS()));
//dest.setEncryption(InventoryEncryption(InventorySSEKMS("yourKmskeyId")));
inventoryConf.setDestination(dest);
/* 清單文件導(dǎo)出的周期, 可選為Daily或者Weekly。*/
inventoryConf.setSchedule(InventoryFrequency::Daily);
/* 是否在清單中包含Object版本信息, 可選為All或者Current。*/
inventoryConf.setIncludedObjectVersions(InventoryIncludedObjectVersions::All);
/* (可選)設(shè)置清單結(jié)果中應(yīng)包含的配置項(xiàng), 請按需配置。*/
InventoryOptionalFields field {
InventoryOptionalField::Size, InventoryOptionalField::LastModifiedDate,
InventoryOptionalField::ETag, InventoryOptionalField::StorageClass,
InventoryOptionalField::IsMultipartUploaded, InventoryOptionalField::EncryptionStatus
};
inventoryConf.setOptionalFields(field);
/* 設(shè)置清單配置。*/
auto outcome = client.SetBucketInventoryConfiguration(
SetBucketInventoryConfigurationRequest(BucketName, inventoryConf));
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Set Bucket Inventory fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* 釋放網(wǎng)絡(luò)等資源。*/
ShutdownSdk();
return 0;
}
查看清單配置
以下代碼用于查看某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫清單規(guī)則名稱。*/
std::string InventoryId = "yourInventoryId";
/* 初始化網(wǎng)絡(luò)等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 獲取清單配置。*/
auto outcome = client.GetBucketInventoryConfiguration(
GetBucketInventoryConfigurationRequest(BucketName, InventoryId));
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Get Bucket Inventory fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* 打印清單配置信息。*/
const auto& inventoryConf = outcome.result().InventoryConfiguration();
std::cout << inventoryConf.Id() << std::endl;
std::cout << inventoryConf.IsEnabled() << std::endl;
std::cout << inventoryConf.Filter().Prefix() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().AccountId() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().RoleArn() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().Bucket() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().Prefix() << std::endl;
if (inventoryConf.Destination().OSSBucketDestination().Encryption().hasSSEKMS()) {
std::cout << inventoryConf.Destination().OSSBucketDestination().Encryption().SSEKMS().KeyId() << std::endl;
}
else if (inventoryConf.Destination().OSSBucketDestination().Encryption().hasSSEOSS()) {
std::cout << "has sse-oss" << std::endl;
}
std::cout << static_cast<int>(inventoryConf.Schedule()) << std::endl;
std::cout << static_cast<int>(inventoryConf.IncludedObjectVersions()) << std::endl;
for (const auto& field: inventoryConf.OptionalFields()) {
std::cout << static_cast<int>(field) << std::endl;
}
/* 釋放網(wǎng)絡(luò)等資源。*/
ShutdownSdk();
return 0;
}
批量列舉清單配置
單次請求最多可獲取100條清單配置項(xiàng)內(nèi)容。若需獲取超過100條清單配置項(xiàng),則需發(fā)送多次請求,并保留相應(yīng)的Token,作為下一次請求的參數(shù)。
以下代碼用于批量列舉某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網(wǎng)絡(luò)等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 設(shè)置請求參數(shù)。*/
std::string nextToken = "";
bool isTruncated = false;
do {
/* 列舉清單配置,每次請求最多返回100條記錄。*/
auto request = ListBucketInventoryConfigurationsRequest(BucketName);
request.setContinuationToken(nextToken);
auto outcome = client.ListBucketInventoryConfigurations(request);
if (!outcome.isSuccess()) {
/*異常處理。*/
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& inventoryConf : outcome.result().InventoryConfigurationList()) {
std::cout << inventoryConf.Id() << std::endl;
std::cout << inventoryConf.IsEnabled() << std::endl;
std::cout << inventoryConf.Filter().Prefix() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().AccountId() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().RoleArn() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().Bucket() << std::endl;
std::cout << inventoryConf.Destination().OSSBucketDestination().Prefix() << std::endl;
if (inventoryConf.Destination().OSSBucketDestination().Encryption().hasSSEKMS()) {
std::cout << inventoryConf.Destination().OSSBucketDestination().Encryption().SSEKMS().KeyId() << std::endl;
}
else if (inventoryConf.Destination().OSSBucketDestination().Encryption().hasSSEOSS()) {
std::cout << "has sse-oss" << std::endl;
}
std::cout << static_cast<int>(inventoryConf.Schedule()) << std::endl;
std::cout << static_cast<int>(inventoryConf.IncludedObjectVersions()) << std::endl;
for (const auto& field: inventoryConf.OptionalFields()) {
std::cout << static_cast<int>(field) << std::endl;
}
}
nextToken = outcome.result().NextContinuationToken();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* 釋放網(wǎng)絡(luò)等資源。*/
ShutdownSdk();
return 0;
}
刪除清單配置
以下代碼用于刪除某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫清單規(guī)則名稱。*/
std::string InventoryId = "yourInventoryId";
/* 初始化網(wǎng)絡(luò)等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運(yùn)行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 刪除清單配置。*/
auto outcome = client.DeleteBucketInventoryConfiguration(
DeleteBucketInventoryConfigurationRequest(BucketName, InventoryId));;
if (!outcome.isSuccess()) {
/* 異常處理。*/
std::cout << "Delete Bucket Inventory fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
}
/* 釋放網(wǎng)絡(luò)等資源。*/
ShutdownSdk();
return 0;
}
相關(guān)文檔
關(guān)于添加存儲空間清單配置的API接口說明,請參見PutBucketInventory。
關(guān)于查看存儲空間清單配置的API接口說明,請參見GetBucketInventory。
關(guān)于批量列舉存儲空間清單配置的API接口說明,請參見ListBucketInventory。
關(guān)于刪除存儲空間清單配置的API接口說明,請參見DeleteBucketInventory。