本文档介绍如何添加、查看、列举和删除存储空间(Bucket)的清单(Inventory)配置。

说明 请确保您拥有调用以上操作的权限。Bucket所有者默认拥有此类权限,若您无此类权限,请先向Bucket所有者申请对应操作的权限。

添加清单配置

说明
  • 单个Bucket最多只能有1000条清单配置。
  • 配置清单的源Bucket与存放导出的清单文件所在的目标Bucket必须位于同一个Region。

以下代码用于为某个Bucket添加清单(Inventory)配置:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    InventoryConfiguration inventoryConf;
    /* 指定的清单名称,清单名称在当前Bucket下必须全局唯一 */
    inventoryConf.setId("inventoryId");

    /* 清单配置是否启用的标识, true或false */
    inventoryConf.setIsEnabled(true);

    /* (可选)清单筛选的前缀。指定前缀后,清单将筛选出符合前缀设置的对象 */
    inventoryConf.setFilter(InventoryFilter("objectPrefix"));

    InventoryOSSBucketDestination dest;
    /* 导出清单文件的文件格式 */
    dest.setFormat(InventoryFormat::CSV);
    /* 存储空间所有者授予的账户ID */
    dest.setAccountId("10988548********");
    /* 存储空间所有者授予操作权限的角色名 */
    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;
        ShutdownSdk();
        return -1;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

有关添加存储空间清单配置的详情,请参见PutBucketInventory

查看清单配置

以下代码用于查看某个Bucket的清单配置:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 清单名称*/
    std::string InventoryId = "yourInventoryId";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* 获取清单配置 */
    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;
        ShutdownSdk();
        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& fiedl: inventoryConf.OptionalFields()) {
        std::cout << static_cast<int>(fiedl) << std::endl;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

有关查看清单配置的详情,请参见GetBucketInventory

批量列举清单配置

说明 单次请求最多可获取100条清单配置项内容。若需获取超过100条清单配置项,则需发送多次请求,并保留相应的token,作为下一次请求的参数。

以下代码用于批量列举某个Bucket的清单配置:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* 设置请求参数*/
    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& fiedl: inventoryConf.OptionalFields()) {
                std::cout << static_cast<int>(fiedl) << std::endl;
            }
        }

        nextToken = outcome.result().NextContinuationToken();
        isTruncated = outcome.result().IsTruncated();
    } while (isTruncated);

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

有关批量列举清单配置的详情,请参见ListBucketInventory

删除清单配置

以下代码用于删除某个Bucket的清单配置:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /* 清单名称*/
    std::string InventoryId = "yourInventoryId";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* 删除清单配置 */
    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;
}

有关删除存储空间清单配置的详情,请参见DeleteBucketInventory