Bucket Operations

Last updated: 2023-09-13 11:03:48

Feature Overview

This document provides an overview of APIs and SDK code samples for basic bucket operations.
API
Operation
Description
Querying the bucket list
Queries the list of all buckets under a specified account
Create a bucket.
Creates a bucket under a specified account
Checking a bucket and its permission
Checks whether a bucket exists and you have permission to access it
Deleting a bucket
Deletes an empty bucket under a specified account

SDK API References

For detailed parameters and method descriptions of all SDK interfaces, please refer to SDK API Reference.

Querying the bucket list

Note

This API (GET Service (List Buckets)) is used to query the list of all buckets under a specified account.

Sample code

GetServiceRequest getServiceRequest = new GetServiceRequest();
cosXmlService.getServiceAsync(getServiceRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetServiceResult getServiceResult = (GetServiceResult) 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.

Create a bucket.

Note

This API (PUT Bucket) is used to create 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";
PutBucketRequest putBucketRequest = new PutBucketRequest(bucket);
cosXmlService.putBucketAsync(putBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
PutBucketResult putBucketResult = (PutBucketResult) 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.

Checking a bucket and its permission

Note

This API (HEAD Bucket) is used to verify whether a bucket exists and whether you have permission to access it.
If the bucket exists and you have permission to read it, HTTP status code 200 will be returned.
If you do not have permission to read the bucket, HTTP status code 403 will be returned.
If the bucket does not exist, HTTP status code 404 will be returned.

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";
HeadBucketRequest headBucketRequest = new HeadBucketRequest(bucket);
cosXmlService.headBucketAsync(headBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
HeadBucketResult headBucketResult = (HeadBucketResult) 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 a bucket

Note

This API (DELETE Bucket) is used to delete a specified bucket.
Note
Before deleting a bucket, ensure that all data and unfinished uploaded parts within the bucket have been cleared; otherwise, the bucket cannot be deleted.

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";
DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucket);
cosXmlService.deleteBucketAsync(deleteBucketRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
DeleteBucketResult deleteBucketResult = (DeleteBucketResult) result;
}

@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.