このトピックでは、バケットインベントリ設定の追加、表示、一覧表示、削除の方法について説明します。
注意事項
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。OSS と同じリージョンにある他の Alibaba Cloud サービスから OSS にアクセスする場合は、内部エンドポイントを使用します。OSS のリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSS エンドポイントを使用して OSSClient インスタンスを作成します。カスタムドメイン名または Security Token Service (STS) を使用して OSSClient を作成する場合は、「OssClient インスタンスの作成」をご参照ください。
バケットインベントリ設定を追加、表示、一覧表示、削除するための権限があることを確認してください。デフォルトでは、バケットのオーナーがこれらの権限を持っています。これらの権限がない場合は、バケットのオーナーに申請してください。
1 つのバケットには、最大 1,000 個のインベントリ設定を含めることができます。
インベントリ設定のソースバケットと、エクスポートされたマニフェストファイルの宛先バケットは、同じリージョンにある必要があります。
インベントリ設定の追加
次のコードは、バケットにインベントリ設定を追加する方法を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* OSS アカウント情報を初期化します。*/
/* yourEndpoint を、バケットが配置されているリージョンのエンドポイントに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、エンドポイントを https://oss-cn-hangzhou.aliyuncs.com に設定します。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion を、バケットが配置されているリージョンに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを cn-hangzhou に設定します。*/
std::string Region = "yourRegion";
/* バケット名を入力します。例: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;
/* インベントリルールの名前を指定します。名前はバケット内でグローバルに一意である必要があります。*/
inventoryConf.setId("inventoryId");
/* インベントリ設定が有効かどうかを指定するフラグ。有効な値:true および false。*/
inventoryConf.setIsEnabled(true);
/* (オプション) オブジェクトをフィルタリングするためのプレフィックス。プレフィックスを指定すると、インベントリにはプレフィックスに一致するオブジェクトのみがリストされます。*/
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");
/* (オプション) マニフェストファイルの暗号化方式。SSE-OSS または SSE-KMS を使用してファイルを暗号化できます。*/
//dest.setEncryption(InventoryEncryption(InventorySSEOSS()));
//dest.setEncryption(InventoryEncryption(InventorySSEKMS("yourKmskeyId")));
inventoryConf.setDestination(dest);
/* マニフェストファイルのエクスポート頻度。有効な値:Daily および Weekly。*/
inventoryConf.setSchedule(InventoryFrequency::Daily);
/* インベントリにオブジェクトバージョン情報を含めるかどうかを指定します。有効な値: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;
}インベントリ設定の表示
次のコードは、バケットのインベントリ設定を表示する方法を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* OSS アカウント情報を初期化します。*/
/* yourEndpoint を、バケットが配置されているリージョンのエンドポイントに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、エンドポイントを https://oss-cn-hangzhou.aliyuncs.com に設定します。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion を、バケットが配置されているリージョンに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを cn-hangzhou に設定します。*/
std::string Region = "yourRegion";
/* バケット名を入力します。例: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;
}インベントリ設定の一覧表示
1 回のリクエストで取得できるインベントリ設定は最大 100 件です。100 件を超えるインベントリ設定を取得するには、複数のリクエストを送信する必要があります。前のリクエストから返されたトークンを、次のリクエストのパラメーターとして使用します。
次のコードは、バケットのインベントリ設定を一覧表示する方法を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* OSS アカウント情報を初期化します。*/
/* yourEndpoint を、バケットが配置されているリージョンのエンドポイントに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、エンドポイントを https://oss-cn-hangzhou.aliyuncs.com に設定します。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion を、バケットが配置されているリージョンに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを cn-hangzhou に設定します。*/
std::string Region = "yourRegion";
/* バケット名を入力します。例: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;
}インベントリ設定の削除
次のコードは、バケットのインベントリ設定を削除する方法を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* OSS アカウント情報を初期化します。*/
/* yourEndpoint を、バケットが配置されているリージョンのエンドポイントに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、エンドポイントを https://oss-cn-hangzhou.aliyuncs.com に設定します。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion を、バケットが配置されているリージョンに設定します。たとえば、バケットが中国 (杭州) リージョンにある場合、リージョンを cn-hangzhou に設定します。*/
std::string Region = "yourRegion";
/* バケット名を入力します。例: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」をご参照ください。