All Products
Search
Document Center

Object Storage Service:Create a bucket (Python SDK V2)

Last Updated:Mar 05, 2026

A bucket is a container for objects in Object Storage Service (OSS). Call the put_bucket method to create a bucket with specific storage settings.

Prerequisites

Before you begin, make sure you have:

  • Python SDK V2 installed

      pip install alibabacloud-oss-v2
  • Credentials configured through the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables EnvironmentVariableCredentialsProvider reads these variables for authentication.

      export OSS_ACCESS_KEY_ID=<your-access-key-id>
      export OSS_ACCESS_KEY_SECRET=<your-access-key-secret>

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account have no permissions by default. Grant permissions through a RAM Policy or bucket policy.

APIActionDescription
PutBucketoss:PutBucketCreates a bucket
oss:PutBucketAclRequired to modify the bucket Access Control List (ACL) after creation

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou). By default, the SDK connects through a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.

Important

Starting from 10:00 (UTC+8) on October 13, 2025, OSS enables Block Public Access by default for new buckets created through the API, OSS SDKs, or ossutil. This change is applied in phases across all regions. For the schedule per region, see the official announcement. After Block Public Access is enabled, you cannot set public ACL permissions (public read or public read/write) or create bucket policies that allow public access. Disable this feature after creation if your workload requires public access.

Sample code

Create a bucket with the Standard storage class

import alibabacloud_oss_v2 as oss

# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

# Load the default SDK configuration and set the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = 'cn-hangzhou'

# Create an OSS client.
client = oss.Client(cfg)

# Create a bucket with the Standard storage class.
try:
    result = client.put_bucket(oss.PutBucketRequest(
        bucket='examplebucket',
        create_bucket_configuration=oss.CreateBucketConfiguration(
            storage_class='Standard'
        )
    ))
    print(f'status code: {result.status_code}, request id: {result.request_id}')
except oss.exceptions.OperationError as e:
    print(f'Operation error: {e}')

This code initializes an OSS client with credentials from environment variables, then calls put_bucket to create a bucket named examplebucket with the Standard storage class. Replace examplebucket with your bucket name and cn-hangzhou with your target region.

Command-line version

Accept the region, bucket name, and endpoint as command-line arguments with argparse.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put bucket sample")
parser.add_argument('--region', help='The region of the bucket.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint for accessing OSS.')

def main():
    args = parser.parse_args()

    # Load credentials from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default SDK configuration.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client.
    client = oss.Client(cfg)

    # Create a bucket with the Standard storage class.
    result = client.put_bucket(oss.PutBucketRequest(
        bucket=args.bucket,
        create_bucket_configuration=oss.CreateBucketConfiguration(
            storage_class='Standard'
        )
    ))
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )


if __name__ == "__main__":
    main()

Method signature

put_bucket(request: PutBucketRequest, **kwargs) -> PutBucketResult

Request parameters

ParameterTypeDescription
requestPutBucketRequestThe request parameters for bucket creation

Return values

TypeDescription
PutBucketResultContains status_code and request_id for the operation result

For the complete method definition, see put_bucket.

References