edit-icon download-icon

Manage a bucket

Last Updated: Nov 03, 2017

A bucket is a namespace on the OSS and a management entity for billing, access control, logging, and other advanced features.

View all buckets

The Client#list_buckets interface is used to list all buckets of the current user. Set the :prefix parameter to list only the buckets with the specific prefix in the name.

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. buckets = client.list_buckets
  6. buckets.each { |b| puts b.name }
  7. buckets = client.list_buckets(:prefix => 'my-')
  8. buckets.each { |b| puts b.name }

Create a bucket

The following code uses the Client#create_bucket interface to create a bucket. Specify the name of the bucket to be created:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. client.create_bucket('my-bucket')

Note:

  • For bucket naming conventions, see Basic OSS Concepts.
  • The bucket name has to be globally unique. Make sure your bucket name is different from the other users’.

Delete a bucket

The following code uses the Client#delete_bucket interface to delete a bucket. Specify the name of the bucket to be deleted:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. client.delete_bucket('my-bucket')

Note:

  • If the bucket to be deleted has objects in it, delete the objects first.
  • If the bucket has an incomplete upload request, use list_uploads and abort_upload to cancel the request before you delete the bucket. For usage, see API Documentation.

Check whether a bucket exists

The following code uses the Client#bucket_exists interface to check whether a specified bucket of the current user exists:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. puts client.bucket_exists?('my-bucket')

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

The following code uses Bucket#acl to view the ACL of a bucket:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. puts bucket.acl

Set the ACL policy of a bucket

The following code uses Bucket#acl= to configure the ACL of a bucket:

  1. require 'aliyun/oss'
  2. client = Aliyun::OSS::Client.new(
  3. endpoint: 'endpoint',
  4. access_key_id: 'AccessKeyId', access_key_secret: 'AccessKeySecret')
  5. bucket = client.get_bucket('my-bucket')
  6. bucket.acl = Aliyun::OSS::ACL::PUBLIC_READ
  7. puts bucket.acl
Thank you! We've received your feedback.