All Products
Search
Document Center

Object Storage Service:Bucket inventory

Last Updated:Oct 19, 2023

This topic describes how to create an inventory for a bucket and how to query, list, and delete the inventories configured for an Object Storage Service (OSS) bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • Make sure that you have the permissions to create, view, list, and delete inventories for a bucket. By default, the bucket owner has the permissions to perform the preceding operations. If you do not have the permissions to perform the preceding operations, contact the bucket owner to grant you the permissions.

  • You can configure up to 1,000 inventories for a bucket.

  • You must deploy the source bucket for which you want to configure an inventory in the same region as the destination bucket in which the inventory list is stored.

Create an inventory for a bucket

The following code provides an example on how to create an inventory for a bucket:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize resources, such as network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    InventoryConfiguration inventoryConf;
    /* Specify the name of the inventory. The name must be globally unique in the current bucket. */
    inventoryConf.setId("inventoryId");

    /* Specify whether to enable inventory for the bucket. Valid values: true and false. */
    inventoryConf.setIsEnabled(true);

    /* (Optional) Specify the prefix in the names of the objects. After you specify the prefix, information about the objects whose names contain the prefix is included in the inventory lists. */
    inventoryConf.setFilter(InventoryFilter("objectPrefix"));

    InventoryOSSBucketDestination dest;
    /* Specify the format of the exported inventory lists. */
    dest.setFormat(InventoryFormat::CSV);
    /* Specify the ID of the Alibaba Cloud account to which the bucket owner grants the permissions to perform the operation. */
    dest.setAccountId("10988548********");
    /* Specify the name of the RAM role to which the bucket owner grants permissions to perform the operation. */
    dest.setRoleArn("acs:ram::10988548********:role/inventory-test");
    /* Specify the bucket in which you want to store the generated inventory lists. */
    dest.setBucket("yourDstBucketName");
    /* Specify the prefix of the path in which you want to store the generated inventory lists. */
    dest.setPrefix("yourPrefix");
    /* (Optional) Specify the method that is used to encrypt inventory lists. Valid values: SSEOSS and SSEKMS. */
    //dest.setEncryption(InventoryEncryption(InventorySSEOSS()));
    //dest.setEncryption(InventoryEncryption(InventorySSEKMS("yourKmskeyId")));
    inventoryConf.setDestination(dest);

    /* Specify the time interval at which inventory lists are exported. Valid values: Daily and Weekly. */
    inventoryConf.setSchedule(InventoryFrequency::Daily);

    /* Specify whether to include all versions of objects or only the current versions of objects in the inventory lists. Valid values: All and Current. */
    inventoryConf.setIncludedObjectVersions(InventoryIncludedObjectVersions::All);

    /* (Optional) Specify the fields that are included in inventory lists based on your requirements. */
    InventoryOptionalFields field { 
        InventoryOptionalField::Size, InventoryOptionalField::LastModifiedDate, 
        InventoryOptionalField::ETag, InventoryOptionalField::StorageClass, 
        InventoryOptionalField::IsMultipartUploaded, InventoryOptionalField::EncryptionStatus
    };
    inventoryConf.setOptionalFields(field);

    /* Configure the inventory. */
    auto outcome = client.SetBucketInventoryConfiguration(
        SetBucketInventoryConfigurationRequest(BucketName, inventoryConf));

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "Set Bucket Inventory fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release resources, such as network resources. */
    ShutdownSdk();
    return 0;
}

Query the inventory configurations of a bucket

The following code provides an example on how to query the inventory configurations of a bucket:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Specify the name of the inventory. */
    std::string InventoryId = "yourInventoryId";

    /* Initialize resources, such as network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    /* Query the inventory configurations. */
    auto outcome = client.GetBucketInventoryConfiguration(
        GetBucketInventoryConfigurationRequest(BucketName, InventoryId));

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "Get Bucket Inventory fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Display information about the inventory. */
    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;
    }

    /* Release resources, such as network resources. */
    ShutdownSdk();
    return 0;
}

List multiple inventories of a bucket at a time

Note

You can query up to 100 inventories in a single request. To query more than 100 inventories, you must send multiple requests and use the token returned for each request as the parameter for the next request.

The following code provides an example on how to list inventories configured for a bucket:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize resources, such as network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    /* Specify request parameters. */
    std::string nextToken = "";
    bool isTruncated = false;
    do {
        /* List the inventories. Up to 100 inventories can be listed per request. */
        auto request = ListBucketInventoryConfigurationsRequest(BucketName);
        request.setContinuationToken(nextToken);
        auto outcome = client.ListBucketInventoryConfigurations(request);

        if (!outcome.isSuccess()) {    
            /* Handle exceptions. */
            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);

    /* Release resources, such as network resources. */
    ShutdownSdk();
    return 0;
}

Delete an inventory of a bucket

The following code provides an example on how to delete an inventory configured for a bucket:

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

int main(void)
{
    /* Initialize information about the account that is used to access OSS. */
            
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Specify the name of the inventory. */
    std::string InventoryId = "yourInventoryId";

    /* Initialize resources, such as network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    /* Delete the inventory. */
    auto outcome = client.DeleteBucketInventoryConfiguration(
        DeleteBucketInventoryConfigurationRequest(BucketName, InventoryId));;

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "Delete Bucket Inventory fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
    }

    /* Release resources, such as network resources. */
    ShutdownSdk();
    return 0;
}

References

  • For more information about the API operation for creating an inventory for a bucket, see PutBucketInventory.

  • For more information about the API operation for querying an inventory configured for a bucket, see GetBucketInventory.

  • For more information about the API operation for listing inventories configured for a bucket, see ListBucketInventory.

  • For more information about the API operation for deleting the inventories configured for a bucket, see DeleteBucketInventory.