本文介紹如何添加、查看、批量列舉和刪除儲存空間(Bucket)的清單(Inventory)配置。
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見地區和Endpoint。
本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OssClient。
請確保您擁有調用添加、查看、列舉和刪除儲存空間清單配置的許可權。Bucket所有者預設擁有此類許可權,如果您無此類許可權,請先向Bucket所有者申請對應操作的許可權。
單個Bucket最多隻能有1000條清單配置。
配置清單的源Bucket與存放匯出的資訊清單檔所在的目標Bucket必須位於同一個Region。
添加清單配置
以下代碼用於為某個Bucket添加清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS帳號資訊。*/
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫Bucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網路等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
InventoryConfiguration inventoryConf;
/* 指定清單規則名稱,該名稱在當前Bucket下必須全域唯一。*/
inventoryConf.setId("inventoryId");
/* 清單配置是否啟用的標識,可選值為true或false。*/
inventoryConf.setIsEnabled(true);
/* (可選)清單篩選的首碼。指定首碼後,清單將篩選出符合首碼的Object。*/
inventoryConf.setFilter(InventoryFilter("objectPrefix"));
InventoryOSSBucketDestination dest;
/* 匯出資訊清單檔的檔案格式。*/
dest.setFormat(InventoryFormat::CSV);
/* 儲存空間擁有者的賬戶UID。*/
dest.setAccountId("10988548********");
/* 指定角色名稱,該角色需要擁有讀取源儲存空間所有檔案以及向目標儲存空間寫入檔案的許可權,格式為acs:ram::uid:role/rolename。*/
dest.setRoleArn("acs:ram::10988548********:role/inventory-test");
/* 存放匯出的資訊清單檔的儲存空間。*/
dest.setBucket("yourDstBucketName");
/* 資訊清單檔的儲存路徑首碼。*/
dest.setPrefix("yourPrefix");
/* (可選)資訊清單檔的加密方式, 可選SSEOSS或者SSEKMS方式加密。*/
//dest.setEncryption(InventoryEncryption(InventorySSEOSS()));
//dest.setEncryption(InventoryEncryption(InventorySSEKMS("yourKmskeyId")));
inventoryConf.setDestination(dest);
/* 資訊清單檔匯出的周期, 可選為Daily或者Weekly。*/
inventoryConf.setSchedule(InventoryFrequency::Daily);
/* 是否在清單中包含Object版本資訊, 可選為All或者Current。*/
inventoryConf.setIncludedObjectVersions(InventoryIncludedObjectVersions::All);
/* (可選)設定清單結果中應包含的配置項, 請按需配置。*/
InventoryOptionalFields field {
InventoryOptionalField::Size, InventoryOptionalField::LastModifiedDate,
InventoryOptionalField::ETag, InventoryOptionalField::StorageClass,
InventoryOptionalField::IsMultipartUploaded, InventoryOptionalField::EncryptionStatus
};
inventoryConf.setOptionalFields(field);
/* 設定清單配置。*/
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;
}
/* 釋放網路等資源。*/
ShutdownSdk();
return 0;
}查看清單配置
以下代碼用於查看某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS帳號資訊。*/
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫Bucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫清單規則名稱。*/
std::string InventoryId = "yourInventoryId";
/* 初始化網路等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數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;
}
/* 釋放網路等資源。*/
ShutdownSdk();
return 0;
}批量列舉清單配置
說明
單次請求最多可擷取100條清單配置項內容。若需擷取超過100條清單配置項,則需發送多次請求,並保留相應的Token,作為下一次請求的參數。
以下代碼用於批量列舉某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS帳號資訊。*/
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫Bucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網路等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 佈建要求參數。*/
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);
/* 釋放網路等資源。*/
ShutdownSdk();
return 0;
}刪除清單配置
以下代碼用於刪除某個Bucket的清單配置:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS帳號資訊。*/
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫Bucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫清單規則名稱。*/
std::string InventoryId = "yourInventoryId";
/* 初始化網路等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數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;
}
/* 釋放網路等資源。*/
ShutdownSdk();
return 0;
}相關文檔
關於添加儲存空間清單配置的API介面說明,請參見PutBucketInventory。
關於查看儲存空間清單配置的API介面說明,請參見GetBucketInventory。
關於批量列舉儲存空間清單配置的API介面說明,請參見ListBucketInventory。
關於刪除儲存空間清單配置的API介面說明,請參見DeleteBucketInventory。