tencent cloud

Cloud Object Storage

Bucket Operations

Download
Modo Foco
Tamanho da Fonte
Última atualização: 2026-05-15 14:54:36

Introduction

This document provides example code and descriptions for querying bucket lists, creating buckets, searching buckets, and deleting buckets using the COS Android SDK.

Must-Knows

Before you perform bucket-related operations, you must have the required permissions. Specifically:
To query bucket lists, set the action to cos:GetService when you configure the authorization policy. For more authorization information, see CAM-supported business APIs.
To create a bucket, set the action to cos:PutBucket when you configure the authorization policy. For more authorization information, see CAM-supported business APIs.
To search for a bucket, set the action to cos:HeadBucket when you configure the authorization policy. For more authorization information, see CAM-supported business APIs.
To delete a bucket, set the action to cos:DeleteBucket when you configure the authorization policy. For more authorization information, see CAM-supported business APIs.

Related Examples

Feature Name
Description
Example code
Querying the Bucket List
Querying the list of all buckets under a specified account.
Creating a Bucket
Create a bucket under a specified account.
Head Bucket
Check whether a bucket exists.
Deleting a Bucket
Delete empty buckets under a specified account.

Preliminary Preparation

Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Creating CosXmlService.

Querying the Bucket List

This operation queries the list of all buckets under a specified account.
GetServiceRequest getServiceRequest = new GetServiceRequest();
cosXmlService.getServiceAsync(getServiceRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetServiceResult getServiceResult = (GetServiceResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Creating a bucket

This API is used to create a bucket (PUT Bucket).
// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
PutBucketRequest putBucketRequest = new PutBucketRequest(bucket);
// Whether the specified bucket is configured with Multi-AZ
putBucketRequest.enableMAZ(false);
cosXmlService.putBucketAsync(putBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
PutBucketResult putBucketResult = (PutBucketResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Retrieving Buckets and Their Permissions

A HEAD Bucket request can verify whether the specified bucket exists and whether you have permission to access it. The following scenarios may occur:
If the bucket exists and you have read permission, the HTTP status code 200 is returned.
If you do not have read permission for the bucket, the HTTP status code 403 is returned.
If the bucket does not exist, the HTTP status code 404 is returned.
// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. https://console.tencentcloud.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 call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Deleting a bucket

Delete the specified bucket (DELETE Bucket).
Note:
Before deleting a bucket, ensure that all data and any incomplete multipart uploads within the bucket are completely cleared. Otherwise, the bucket cannot be deleted.
// A bucket name consists of bucketname-appid. You must include the appid. You can view bucket names in the COS console. https://console.tencentcloud.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();
}
}
});

SDK API Reference

For detailed parameters and method descriptions of all SDK interfaces, see SDK API Reference.

API Operations

For the description of the API for listing buckets, see the GET Service (List Buckets) document.
For the description of the API for creating a bucket, see the PUT Bucket document.
For the description of the API for checking a buckets, see the HEAD Bucket document.
For the description of the API for deleting a bucket, see the DELETE Bucket document.

Ajuda e Suporte

Esta página foi útil?

comentários