Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer les inventaires de bucket à l'aide du SDK OSS pour C++

Dernière mise à jour :Aug 18, 2026

Vous pouvez créer, interroger, répertorier et supprimer des configurations d'inventaire pour un bucket Object Storage Service (OSS).

Notes d'utilisation

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.

  • Assurez-vous de disposer des autorisations nécessaires pour créer, afficher, répertorier et supprimer les inventaires d'un bucket. Par défaut, le propriétaire du bucket détient ces autorisations. Si vous ne disposez pas des autorisations requises, contactez le propriétaire du bucket pour qu'il vous les accorde.

  • Vous pouvez configurer jusqu'à 1 000 inventaires pour un seul bucket.

  • Le bucket source et le bucket de destination qui stocke les listes d'inventaire doivent se trouver dans la même région.

Créer un inventaire pour un bucket

L'exemple de code suivant montre comment créer un inventaire pour un 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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);
    client.SetRegion(Region);

    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;
}

Interroger les configurations d'inventaire d'un bucket

L'exemple de code suivant interroge les configurations d'inventaire d'un 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* 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;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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);
    client.SetRegion(Region);

    /* 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;
}

Répertorier plusieurs inventaires d'un bucket en une seule fois

Remarque

Vous pouvez interroger jusqu'à 100 inventaires dans une seule requête. Pour interroger plus de 100 inventaires, vous devez envoyer plusieurs requêtes et utiliser le jeton renvoyé par chaque requête comme paramètre pour la requête suivante.

L'exemple de code suivant montre comment répertorier les inventaires configurés pour un 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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);
    client.SetRegion(Region);

    /* 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;
}

Supprimer un inventaire d'un bucket

L'exemple de code suivant montre comment supprimer un inventaire configuré pour un 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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* 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;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 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);
    client.SetRegion(Region);

    /* 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;
}

Références

  • Pour plus d'informations sur l'opération API permettant de créer un inventaire pour un bucket, consultez PutBucketInventory.

  • Pour plus d'informations sur l'opération API permettant d'interroger un inventaire configuré pour un bucket, consultez GetBucketInventory.

  • Pour plus d'informations sur l'opération API permettant de répertorier les inventaires configurés pour un bucket, consultez ListBucketInventory.

  • Pour plus d'informations sur l'opération API permettant de supprimer les inventaires configurés pour un bucket, consultez DeleteBucketInventory.