Feature Overview
This document provides an overview of APIs and SDK code samples for basic bucket operations.
API | Operation | Description |
Querying a bucket list | Queries the list of all buckets under a specified account | |
Creating 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 |
Querying a bucket list
Note
This API (
GET Service (List Buckets)) is used to query the list of all buckets under a specified account.Sample code
try {let listAllMyBuckets: ListAllMyBuckets = await Cos.getDefaultService().getService();For bucket list details, please refer to the ListAllMyBuckets class.} catch (e) {// An exception will be thrown upon failure, handle the error accordinglyconsole.log(e);}
Description
-
Response description
Success:
ListAllMyBuckets is returned, including the list of buckets and bucket owner information.Failure: An error occurs (e.g., authentication failure), and an exception CosXmlClientError or CosXmlServiceError is thrown. For more information, please refer to Error Handling.
Parameters in the
ListAllMyBuckets response body are as follows:Parameter name | Description | Local Disk Types |
buckets | A buckets list | List<Bucket> |
owner | Bucket owner information | Owner |
Bucket has the following sub-nodes:Parameter name | Description | Local Disk Types |
name | Bucket name | String |
location | Bucket region | String |
createDate | Bucket creation time in ISO 8601 format. Example: 2019-05-24T10:56:40Z | String |
Owner has the following sub-nodes:Parameter name | Description | Local Disk Types |
id | Complete ID | String |
disPlayName | Bucket owner name | String |
Creating Bucket
Note
This API (
PUT Bucket) is used to create a bucket.Sample code
// Bucket name in the format of <1>BucketName-APPID</1> (<3>APPID</3> is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.let bucket = "examplebucket-1250000000";// Bucket region abbreviation, e.g. ap-guangzhoulet region = "COS_REGION";// Whether to enable multi-AZlet enableMAZ = false;try {await Cos.getDefaultService().putBucket(bucket,{region: region,enableMAZ: enableMAZ});} catch (e) {// An exception will be thrown upon failure, handle the error accordinglyconsole.log(e);}
Description
Parameter name | Description | Local Disk Types | Required |
bucket | Bucket name follows the naming convention BucketName-APPID. For more information, please refer to Bucket Overview. | String | Required |
enableMAZ | Whether to create an MAZ bucket | String | Not required |
Response description
Success: no value is returned.
Failure: An error occurs (e.g., authentication failure), and an exception CosXmlClientError or CosXmlServiceError is thrown. For more information, please refer to Error Handling.
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 <1>BucketName-APPID</1> (<3>APPID</3> is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.let bucket = "examplebucket-1250000000";// Bucket region abbreviation, e.g. ap-guangzhoulet region = "COS_REGION";try {let header = await Cos.getDefaultService().headBucket(bucket,region);// HTTP status code is 200, HTTP header is header} catch (e) {// Refer to e.statusCode for the specific HTTP status codeconsole.log(e);}
Description
Parameter name | Description | Local Disk Types |
bucket | Bucket name follows the naming convention BucketName-APPID. For more information, please refer to Bucket Overview. | String |
Response description
Success: The HTTP Header is returned.
Failure: An error occurs (e.g., authentication failure), and an exception CosXmlClientError or CosXmlServiceError is thrown. For more information, please refer to Error Handling.
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 <1>BucketName-APPID</1> (<3>APPID</3> is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.let bucket = "examplebucket-1250000000";// Bucket region abbreviation, e.g. ap-guangzhoulet region = "COS_REGION";try {await Cos.getDefaultService().deleteBucket(bucket,region);} catch (e) {// An exception will be thrown upon failure, handle the error accordinglyconsole.log(e);}
Description
Parameter name | Description | Local Disk Types |
bucket | Bucket name follows the naming convention BucketName-APPID. For more information, please refer to Bucket Overview. | String |
Response description
Success: no value is returned.
Failure: An error occurs (e.g., authentication failure), and an exception CosXmlClientError or CosXmlServiceError is thrown. For more information, please refer to Error Handling.