Bucket management
Updated at:2025-11-03
Create Bucket
-
Basic workflow
- Create a BosClient instance.
- To use the createBucket() method, you must provide the name of the bucket.
-
Example code
JavaScript1let newBucketName = <BucketName>; //reate a new bucket and specify the bucket name 2client.createBucket(newBucketName) 3 .then(function() { 4 // Creation completed, add your own code; 5 }) 6 .catch(function(error) { 7 // Creation failed, add your own code to handle the exception 8 });Plain Text1> **Note:** Since the bucket name is unique across all regions, it is necessary to ensure that the BucketName is not the same as the BucketName on all other regions.
Set bucket storage class
A newly created bucket is of the standard storage class by default. You can set or get the storage class of a specified bucket through the following code:
- Basic workflow
- Create a BosClient
- putBucketStorageclass() method
| Parameters | Description |
|---|---|
| bucketName | Bucket name |
| storageClass | Storage classes supported: 'STANDARD' (standard storage class), 'STANDARD_IA' (infrequent access storage), 'ARCHIVE' (archive storage), 'COLD' (cold storage), 'MAZ_STANDARD' (standard storage - multi-AZ), 'MAZ_STANDARD_IA' (infrequent access storage - multi-AZ) |
-
Example code
JavaScript1client.putBucketStorageclass(bucketName,storageClass) 2 .then(function() { 3 // Setup completed 4 }) 5 .catch(function(error) { 6 // Setup failed 7 });
View bucket list
-
Basic workflow
- Create a BosClient instance.
- Run the listBuckets() method.
-
Example code
The following code can list all buckets of a user:
JavaScript1client.listBuckets() 2 .then(function(response) { 3 (response.body.buckets || []).forEach(function (bucket) { console.log(bucket.name) }) 4 }) 5 .catch(function() { 6 // Query failed, add your own code to handle the exception 7 });
Check if bucket exists
-
Basic workflow
- Create a BosClient instance.
- Run the doesBucketExist() method.
-
Example code
JavaScript1client.doesBucketExist(<BucketName>) //Specify the bucket name 2 .then(function(response) { 3 if(response) { 4 console.log('Bucket exists'); 5 } 6 else { 7 console.log('Bucket not exists'); 8 } 9 }) 10 .catch(function() { 11 // Query failed, add your own code to handle the exception 12 });
Delete bucket
-
Basic workflow
- Create a BosClient instance.
- Run the deleteBucket() method.
-
Example code
JavaScript1client.deleteBucket(<BucketName>) 2 .then(function() { 3 // Deletion completed 4 }) 5 .catch(function(error) { 6 // Deletion failed 7 });Plain Text1> **Note:** A bucket cannot be deleted if it is not empty (i.e., it contains objects or unfinished multipart uploads). The bucket must be emptied before successful deletion.
Bucket permission control
Set bucket access permission
-
Basic workflow
- Create a BosClient instance.
- Run the setBucketAcl() method.
-
Example code
JavaScript1//Set the bucket access permission as private 2client.setBucketCannedAcl(<BucketName>, 'private') 3 .then(function() { 4 // Setup completed 5 }) 6 .catch(function(error) { 7 // Setup failed 8 });Plain Text1> **Note:** Bucket access permissions include three values: `private`, `public-read`, `public-read-write` , which correspond to respective permissions. For details, refer to BOS API Documentation - [Permission Control Using CannedAcl](BOS/API Reference/Access control.md#Permission control by CannedAcl).
Set access permissions for a specific user on the bucket
-
Basic workflow
- Create a BosClient instance.
- Run the setBucketAcl() method.
-
Example code
JavaScript1let grant_list = [ 2 { 3 'grantee': [ 4 {'id': <UserID1>}, // Grant permission to specific user 1 5 {'id': <UserID2>}, // Grant permission to specific user 2 6 ], 7 'permission': ['FULL_CONTROL'] // Set permission to FULL_CONTROL 8 }, 9 { 10 'grantee': [ 11 {'id': <UserID3>}, // Grant permission to specific user 3 12 ], 13 'permission': ['READ'] // Set permission to READ 14 } 15]; 16client.setBucketAcl(<BucketName>, grant_list) 17 .then(function() { 18 // Setup completed 19 }) 20 .catch(function(error) { 21 // Setup failed 22 });Plain Text1> **Note**: The permission settings in permission include three values: `READ`, `WRITE` and `FULL_CONTROL`, which correspond to relevant permissions respectively. For details, refer to BOS API Documentation - [Permission Control via Uploading ACL Files](BOS/API Reference/Access control.md#Permission control by uploading ACL files).
