Bucket Management
Create Bucket
-
Basic procedure
1.Create BosClient 2.Execute createbucket() method, and you need to provide bucket name.
-
Sample Code
let newBucketName =<BucketName>; // Create a bucket and specify the name of the bucket client.createbucket(newBucketName) .then(function() { // Created successfully, add your own code; }) .catch(function(error) { // Failed to create, add your own code to handle exception });
Note: Since the name of the bucket is unique in all regions, you need to make sure that the BucketName is different from that on all other regions.
View Bucket List
-
Basic procedure
1.Create BosClient 2.List listbuckets() method
-
Sample Code
The following code can list all the users' buckets:
client.listBuckets() .then(function(response) { (response.body.buckets || []).forEach(function (bucket) { console.log(bucket.name) }) }) .catch(function() { // If inquiry fails, add your own code to handle exception });
Judge whether a Bucket Exists or Not
-
Basic procedure
1.Create BosClient 2.Execute doesBucketExist() method
-
Sample Code
client.doesBucketExist(<BucketName>) //Designate the name of the bucket: .then(function(response) { if(response) { console.log('Bucket exists'); } else { console.log('Bucket not exists'); } }) .catch(function() { // If inquiry fails, add your own code to handle exception });
Delete Bucket
-
Basic procedure
- Create BosClient
- Execute deletebucket() method
-
Sample Code
client.deleteBucket(<BucketName>) .then(function() { // Deleted successfully }) .catch(function(error) { // Failed to delete });
Note: If the bucket is not null (i.e. bucket has object and unfinished three-step upload Parts), the bucket cannot be deleted and must be emptied to be deleted successfully.
Bucket Privilege Control
Set Access Privileges of Bucket
-
Basic procedure
1.Create BosClient 2.Execute setBucketAcl() method
-
Sample Code
// Set access privilege of bucket as private client.SetBucketCannedAcl(<BucketName>, 'private') .then(function() { // Set successfully }) .catch(function(error) { // Failed to set });
Note: bucket access privilege contains three values:
private
,public-read
,public-read-write
. They correspond to the respective privileges. For specific contents, please see "BOS API Document Privilege Control through CannedAcl.
Set the Specified User's Access to the Bucket
-
Basic procedure
1.Create BosClient 2.Execute setBucketAcl() method
-
Sample Code
let grant_list = [ { 'grantee': [ {'id':<UserID1>}, // privilege to specific users 1 {'id':<UserID2>}, // privilege to specific users 2 ], 'privilege': ['FULL_CONTROL'] // Set privilege as FULL_CONTROL }, { 'grantee': [ {'id':<UserID3>} // privilege to specific users 3 ], 'privilege': ['READ'] // Set privilege as READ } ]; client.setBucketAcl(<BucketName>, grant_list) .then(function() { // Set successfully }) .catch(function(error) { // Failed to set });
Note: The privilege setting in privilege has three values:
READ
,WRITE
andFULL_CONTROL
, corresponding to respective privileges. For the specific contents, please see <BOS API Document Privilege Control through ACL File Uploading>.