Create buckets using OSS SDK for Python 2.0
A bucket is a container for objects. Learn how to create a bucket using OSS SDK for Python 2.0.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
PutBucket |
|
Creates a bucket. |
|
|
After creating a bucket, this permission is required to modify the bucket ACL. |
Notes
-
The sample code uses the region ID
cn-hangzhou(China (Hangzhou)). By default, the public endpoint is used. To access resources from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see Regions and endpoints. -
This sample creates a bucket with a regional attribute. To create a bucket without a regional attribute, replace the endpoint with
https://oss-rg-china-mainland.aliyuncs.com. Buckets without regional attributes support only Standard storage class and locally redundant storage for disaster recovery, and cannot use resource groups. Bucket regional attributes. -
Starting at 10:00:00 (UTC+8) on October 13, 2025, OSS gradually enables Block public access by default for buckets that are created by calling API operations or by using OSS SDKs or ossutil in all regions. For the time when the change takes effect in each region, see Announcement. After this feature is enabled, you cannot grant public access permissions, including the public-read or public-read-write ACL and bucket policies that allow public access. If your business requires public access, you can disable block public access after the bucket is created.
Method
put_bucket(request: PutBucketRequest, **kwargs) → PutBucketResult
Request parameter
|
Parameter |
Type |
Description |
|
request |
PutBucketRequest |
The request parameter. PutBucketRequest. |
Response parameter
|
Type |
Description |
|
PutBucketResult |
The response. PutBucketResult. |
For the full API, see put_bucket.
Sample code
The following sample code creates a bucket with Standard storage class.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser.
parser = argparse.ArgumentParser(description="put bucket sample")
# Specify the required command line parameter --region, which specifies the region in which the bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the required command line parameter --bucket, which specifies the name of the bucket.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the optional command line parameter --endpoint, which specifies the endpoint that other services can use to access OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args() # Parse command line parameters.
# Load access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the bucket is located.
cfg.region = args.region
# If the endpoint parameter is provided, specify the endpoint that other services can use to access OSS.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configurations to create an OSSClient instance.
client = oss.Client(cfg)
# Execute the request to create a bucket and set its storage class to Standard.
result = client.put_bucket(oss.PutBucketRequest(
bucket=args.bucket,
create_bucket_configuration=oss.CreateBucketConfiguration(
storage_class='Standard'
)
))
# Output the HTTP status code in the response and the request ID used to check whether the request is successful.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main() # Entry point of the script. The main function is invoked when the file is run directly.
References
-
Complete sample code: put_bucket.py.