A bucket is a namespace on the OSS and a management entity for billing, access control, logging, and other advanced features.
You can use oss2.BucketIterator to traverse all the buckets:
# -*- coding: utf-8 -*-
auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
service = oss2.Service(auth, 'Your endpoint')
print([b.name for b in oss2.BucketIterator(service)])
Specifically, the oss2.Service is used to access “OSS service”-related classes. Currently, it is only used to list the user’s buckets.
You can create a new bucket in a specified region by specifying an endpoint and a bucket name:
bucket = oss2.Bucket (auth, 'Your endpoint', 'your bucket name')
For example, when you set the endpoint to
http://oss-cn-beijing.aliyuncs.com, you can create a bucket in the Beijing region.
For information about endpoints, regions and their mapping relationships as well as bucket naming conventions, refer to OSS Basic Concepts.
You can specify the bucket ACL when creating a bucket. For example, you can use the following code to create a public-read bucket:
You can use the following code to delete an empty bucket.
print('bucket is not empty.')
print('bucket does not exist')
If the bucket to be deleted is not empty (an object or a part in the bucket is being uploaded), the bucket cannot be deleted, and the SDK will throw the BucketNotEmpty exception. If the bucket to be deleted does not exist, the SDK will throw the NoSuchBucket exception.
- Once a bucket is deleted, the name of the bucket may be applied for by other users.
- If a bucket is not empty, you can list and delete objects in the bucket (terminate multipart upload if there is any) to clear the bucket, and then delete the bucket.
The following code sets the access permission of a bucket to private: