edit-icon download-icon

Manage a bucket

Last Updated: Oct 31, 2017

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.

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });
  8. co(function* () {
  9. var result = yield client.listBuckets();
  10. console.log(result);
  11. var result = yield client.listBuckets({
  12. prefix: 'prefix'
  13. });
  14. console.log(result);
  15. }).catch(function (err) {
  16. console.log(err);
  17. });

Create a bucket

You can create a bucket using the ‘putBucket’ interface. You require to specify the bucket name:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });
  8. co(function* () {
  9. var result = yield client.putBucket('bucket name');
  10. console.log(result);
  11. }).catch(function (err) {
  12. console.log(err);
  13. });

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:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });
  8. co(function* () {
  9. var result = yield client.deleteBucket('bucket name');
  10. console.log(result);
  11. }).catch(function (err) {
  12. console.log(err);
  13. });

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:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });
  8. co(function* () {
  9. var result = yield client.getBucketACL('bucket name');
  10. console.log(result);
  11. }).catch(function (err) {
  12. console.log(err);
  13. });

Set the ACL policy of a bucket

You can use ‘putBucketACL’ to configure the ACL policy of a bucket:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>'
  7. });
  8. co(function* () {
  9. var result = yield client.putBucketACL('bucket name', 'region', 'public-read');
  10. console.log(result);
  11. }).catch(function (err) {
  12. console.log(err);
  13. });
Thank you! We've received your feedback.