Inventory

Last updated: 2023-09-13 11:11:40

Feature Overview

This document provides an overview of APIs and SDK code samples for COS inventory.
API
Operation
Description
Setting an inventory job
Sets an inventory job for a bucket
Querying Inventory Jobs
Queries the inventory jobs of a bucket
Deleting an Inventory Job
Deletes an inventory job from a bucket

SDK API References

For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.

Setting an inventory job

Note

This API (PUT Bucket inventory) is used to create an inventory job for a bucket.

Sample code

// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
String bucket = "examplebucket-1250000000";
PutBucketInventoryRequest putBucketInventoryRequest =
new PutBucketInventoryRequest(bucket);
putBucketInventoryRequest.setInventoryId("exampleInventoryId");
// Indicate whether to include object versions in the inventory:
// If set to All, all object versions are included in the inventory,
// with additional fields VersionId, IsLatest, and DeleteMarker
// If set to Current, no object versions are included in the inventory
putBucketInventoryRequest.setIncludedObjectVersions(InventoryConfiguration
.IncludedObjectVersions.ALL);
// Backup frequency
putBucketInventoryRequest.setScheduleFrequency(InventoryConfiguration
.SCHEDULE_FREQUENCY_DAILY);
// Backup path
putBucketInventoryRequest.setDestination("CSV", "1000000000",
"examplebucket-1250000000", "region", "dir/");

cosXmlService.putBucketInventoryAsync(putBucketInventoryRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
PutBucketInventoryResult putBucketInventoryResult =
(PutBucketInventoryResult) result;
}

// If you are using Kotlin to call the API, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called, i.e.:
// Is the type of clientException CosXmlClientException? Is the type of serviceException CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
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 Codes
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 may not have access to the bucket
HTTP 403 Forbidden

Querying Inventory Jobs

Note

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

Sample code

// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
String bucket = "examplebucket-1250000000";
GetBucketInventoryRequest getBucketInventoryRequest =
new GetBucketInventoryRequest(bucket);
getBucketInventoryRequest.setInventoryId("exampleInventoryId");

cosXmlService.getBucketInventoryAsync(getBucketInventoryRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketInventoryResult getBucketInventoryResult =
(GetBucketInventoryResult) result;
}

// If you are using Kotlin to call the API, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called, i.e.:
// Is the type of clientException CosXmlClientException? Is the type of serviceException CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note
For the complete sample, go to GitHub.

Deleting an Inventory Job

Note

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

Sample code

// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
String bucket = "examplebucket-1250000000";
DeleteBucketInventoryRequest deleteBucketInventoryRequest =
new DeleteBucketInventoryRequest(bucket);
deleteBucketInventoryRequest.setInventoryId("exampleInventoryId");

cosXmlService.deleteBucketInventoryAsync(deleteBucketInventoryRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
DeleteBucketInventoryResult deleteBucketInventoryResult =
(DeleteBucketInventoryResult) result;
}

// If you are using Kotlin to call the API, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called, i.e.:
// Is the type of clientException CosXmlClientException? Is the type of serviceException CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
}
});
Note
For the complete sample, go to GitHub.