All Products
Search
Document Center

Object Storage Service:Create buckets using OSS SDK for Python 2.0

Last Updated:Jun 03, 2026

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

oss:PutBucket

Creates a bucket.

oss:PutBucketAcl

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.

  • Starting October 13, 2025, at 10:00 (UTC+8), OSS begins a phased rollout to enable blocking public access by default for all new buckets. This change applies to buckets created through the API, SDKs, and ossutil. For the specific rollout schedule in each region, see the Notice. When this feature is enabled, you cannot grant public access to the bucket, either through ACLs (such as public-read or public-read-write) or bucket policies. If your use case requires public access, disable this setting 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