Inventory

Last updated: 2020-09-09 11:34:36

    Overview

    This document provides an overview of APIs and SDK code samples related to COS inventory.

    API Operation Description
    PUT Bucket inventory Setting an inventory job Sets an inventory job for a bucket
    GET Bucket inventory Querying inventory jobs Queries the inventory jobs of bucket
    DELETE Bucket inventory Deleting an inventory job Deletes an inventory job from a bucket

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, please see SDK API Reference.

    Setting an Inventory Job

    API description

    This API is used to create an inventory job for a bucket.

    Sample code

    Objective-C

    QCloudPutBucketInventoryRequest *putReq = [QCloudPutBucketInventoryRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    putReq.bucket= @"examplebucket-1250000000";
    
    // ID of the inventory job
    putReq.inventoryID = @"list1";
    
    // You can use XML to set specific configuration information for the inventory job in the request body, including the objects to be analyzed,
    // analysis frequency, analysis dimensions, result format, and storage location.
    QCloudInventoryConfiguration *config = [QCloudInventoryConfiguration new];
    
    // Inventory ID, corresponding to the ID in the request parameter
    config.identifier = @"list1";
    
    // Specifies whether inventory is enabled:
    // if it is set to `true`, inventory is enabled;
    // if `false`, no inventories will be generated
    config.isEnabled = @"True";
    
    // Information on storage of the inventory result
    QCloudInventoryDestination *des = [QCloudInventoryDestination new];
    
    QCloudInventoryBucketDestination *btDes =[QCloudInventoryBucketDestination new];
    
    // File format of the inventory result. Valid value: CSV
    btDes.cs = @"CSV";
    
    // ID of the bucket owner
    btDes.account = @"1278687956";
    
    // Bucket name in the format: `BucketName-APPID`
    btDes.bucket  = @"qcs::cos:ap-guangzhou::examplebucket-1250000000";
    
    // Prefix of the inventory result
    btDes.prefix = @"list1";
    
    // Encryption with a COS-managed key
    QCloudInventoryEncryption *enc = [QCloudInventoryEncryption new];
    enc.ssecos = @"";
    
    // Option to provide server-side encryption for the inventory result
    btDes.encryption = enc;
    
    // Information on the bucket where the exported inventory result is stored
    des.bucketDestination = btDes;
    
    // Information on storage of the inventory result
    config.destination = des;
    
    // Configure the frequency of the inventory job
    QCloudInventorySchedule *sc = [QCloudInventorySchedule new];
    
    // Inventory job frequency. Enumerated values: Daily, Weekly
    sc.frequency = @"Daily";
    config.schedule = sc;
    QCloudInventoryFilter *fileter = [QCloudInventoryFilter new];
    fileter.prefix = @"myPrefix";
    config.filter = fileter;
    config.includedObjectVersions = QCloudCOSIncludedObjectVersionsAll;
    QCloudInventoryOptionalFields *fields = [QCloudInventoryOptionalFields new];
    
    fields.field = @[ @"Size",
                      @"LastModifiedDate",
                      @"ETag",
                      @"StorageClass",
                      @"IsMultipartUploaded",
                      @"ReplicationStatus"];
    
    // Set the analysis items that should be included in the inventory result
    config.optionalFields = fields;
    putReq.inventoryConfiguration = config;
    [putReq setFinishBlock:^(id outputObject, NSError *error) {
        // “outputObject” returns information such as the Etag or custom headers in the response
        NSDictionary * result = (NSDictionary *)outputObject;
    
    }];
    [[QCloudCOSXMLService defaultCOSXML] PutBucketInventory:putReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let putReq = QCloudPutBucketInventoryRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    putReq.bucket = "examplebucket-1250000000";
    
    // ID of the inventory job
    putReq.inventoryID = "list1";
    
    // You can use XML to set specific configuration information for the inventory job in the request body, including the objects to be analyzed by the inventory job,
    // analysis frequency, analysis dimensions, result format, and storage location.
    let config = QCloudInventoryConfiguration.init();
    
    // Inventory ID, corresponding to the ID in the request parameter
    config.identifier = "list1";
    
    // Specifies whether inventory is enabled:
    // if it is set to `true`, inventory is enabled;
    // if `false`, no inventories will be generated
    config.isEnabled = "True";
    
    // Information on storage of the inventory result
    let des = QCloudInventoryDestination.init();
    let btDes = QCloudInventoryBucketDestination.init();
    
    // File format of the inventory result. Valid value: CSV
    btDes.cs = "CSV";
    
    // ID of the bucket owner
    btDes.account = "1278687956";
    
    // Name of the bucket where the inventory result is stored
    btDes.bucket  = "qcs::cos:ap-guangzhou::examplebucket-1250000000";
    
    // Prefix of the inventory result
    btDes.prefix = "list1";
    
    // Encryption with a COS-managed key
    let enc = QCloudInventoryEncryption.init();
    enc.ssecos = "";
    
    // Option to provide server-side encryption for the inventory result
    btDes.encryption = enc;
    
    // Information on the bucket where the exported inventory result is stored
    des.bucketDestination = btDes;
    
    // Information on storage of the inventory result
    config.destination = des;
    
    // Configure the frequency of the inventory job
    let sc = QCloudInventorySchedule.init();
    
    // Inventory job frequency. Enumerated values: Daily, Weekly
    sc.frequency = "Daily";
    config.schedule = sc;
    let fileter = QCloudInventoryFilter.init();
    fileter.prefix = "myPrefix";
    config.filter = fileter;
    config.includedObjectVersions = .all;
    let fields = QCloudInventoryOptionalFields.init();
    fields.field = [ "Size",
                     "LastModifiedDate",
                     "ETag",
                     "StorageClass",
                     "IsMultipartUploaded",
                     "ReplicationStatus"];
    // Set the analysis items that should be included in the inventory result
    config.optionalFields = fields;
    putReq.inventoryConfiguration = config;
    
    putReq.finishBlock = {(result,error) in
        if let result = result {
            // “result” contains response headers
        } else {
            print(error!);
        }
    }
    
    QCloudCOSXMLService.defaultCOSXML().putBucketInventory(putReq);

    Note:

    For the complete sample, go to GitHub.

    Error codes

    The following describes some common errors that may occur when making requests using this API.

    Error Code Description Status Code
    InvalidArgument Invalid parameter value HTTP 400 Bad Request
    TooManyConfigurations The number of inventories has reached the upper limit of 1,000 HTTP 400 Bad Request
    AccessDenied Unauthorized access. You most likely do not have access to the bucket. HTTP 403 Forbidden

    Querying Inventory Jobs

    API description

    This API is used to query the inventory jobs of a bucket.

    Sample code

    Objective-C

    QCloudGetBucketInventoryRequest *getReq = [QCloudGetBucketInventoryRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    getReq.bucket = @"examplebucket-1250000000";
    
    // ID of the inventory job
    getReq.inventoryID = @"list1";
    [getReq setFinishBlock:^(QCloudInventoryConfiguration * _Nonnull result,
                             NSError * _Nonnull error) {
        // `result` contains the inventory information
    }];
    [[QCloudCOSXMLService defaultCOSXML] GetBucketInventory:getReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let req = QCloudGetBucketInventoryRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    // ID of the inventory job
    req.inventoryID = "list1";
    req.setFinish {(result,error) in
        if let result = result {
            // Information on the job
            let enabled = result.isEnabled
        } else {
            print(error!);
        }
    }
    QCloudCOSXMLService.defaultCOSXML().getBucketInventory(req);

    Note:

    For the complete sample, go to GitHub.

    Deleting an Inventory Job

    API description

    This API is used to delete a specified inventory job from a bucket.

    Sample code

    Objective-C

    QCloudDeleteBucketInventoryRequest *delReq = [QCloudDeleteBucketInventoryRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    delReq.bucket = @"examplebucket-1250000000";
    
    // ID of the inventory job
    delReq.inventoryID = @"list1";
    [delReq setFinishBlock:^(id outputObject, NSError *error) {
        // “outputObject” returns information such as the Etag or custom headers in the response
        NSDictionary * result = (NSDictionary *)outputObject;
    
    }];
    [[QCloudCOSXMLService defaultCOSXML] DeleteBucketInventory:delReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let delReq = QCloudDeleteBucketInventoryRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    delReq.bucket = "examplebucket-1250000000";
    
    // ID of the inventory job
    delReq.inventoryID = "list1";
    delReq.finishBlock = {(result,error) in
        if let result = result {
            // “result” contains response headers
        } else {
            print(error!);
        }
    }
    
    QCloudCOSXMLService.defaultCOSXML().deleteBucketInventory(delReq);

    Note:

    For the complete sample, go to GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help