A bucket is a namespace on OSS and a management entity for billing, access control, logging, and other advanced features.
View all buckets
You can use the ‘listBuckets’ interface to list all the buckets belonging to the current user. You can specify the ‘prefix’ parameter to list all buckets whose names contain the specified prefix.
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});
co(function* () {
var result = yield client.listBuckets();
console.log(result);
var result = yield client.listBuckets({
prefix: 'prefix'
});
console.log(result);
}).catch(function (err) {
console.log(err);
});
Create a bucket
You can create a bucket using the ‘putBucket’ interface. You require to specify the bucket name:
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});
co(function* () {
var result = yield client.putBucket('bucket name');
console.log(result);
}).catch(function (err) {
console.log(err);
});
Note:
For bucket naming conventions, see OSS Basic Concepts.
This is because the name of a bucket is globally unique, make sure your bucket name does not match with the other users’.
Delete a bucket
You can use the ‘deleteBucket’ interface to delete a bucket. You require to specify the bucket name:
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});
co(function* () {
var result = yield client.deleteBucket('bucket name');
console.log(result);
}).catch(function (err) {
console.log(err);
});
Note:
If the bucket to be deleted has objects in it, you must delete the objects first.
If the bucket to be deleted has unfinished upload requests, you must cancel the requests first using the ‘listUploads’ and‘abortMultipartUpload’ interfaces.
Bucket access permissions
You can set the ACL policy of a bucket to allow or prohibit anonymous reads/writes to the bucket. For more information on ACL, see ACL.
Obtain the ACL of a bucket
You can use ‘getBucketACL’ to view the ACL policy of a bucket:
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});
co(function* () {
var result = yield client.getBucketACL('bucket name');
console.log(result);
}).catch(function (err) {
console.log(err);
});
Set the ACL policy of a bucket
You can use ‘putBucketACL’ to configure the ACL policy of a bucket:
var co = require('co');
var OSS = require('ali-oss');
var client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>'
});
co(function* () {
var result = yield client.putBucketACL('bucket name', 'region', 'public-read');
console.log(result);
}).catch(function (err) {
console.log(err);
});