All Products
Search
Document Center

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

Last Updated:Jul 31, 2025

A bucket is a container for storing objects. When you upload files, they are stored as objects in buckets. This topic describes how to create a bucket using Python SDK V2.

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 Policy or Bucket Policy.

API

Action

Definition

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 in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public Endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, use an internal Endpoint. For more information about the regions and Endpoints that OSS supports, see Regions and endpoints.

  • From 10:00 (UTC+8) on October 13, 2025, OSS will implement a phased adjustment across all regions to enable Block Public Access by default for new buckets created using the API, OSS SDKs, or ossutil. For details about the exact times when the adjustment will take effect in each region, see [Official Announcement] Adjustment to Public Access Blocking Configurations for Newly Created Buckets. Once Block Public Access is enabled, you cannot configure public access permissions, including public ACLs (public read and public read/write) and bucket policies that allows public access. You can disable this feature after the bucket is created if your business requires public access.

Method definition

put_bucket(request: PutBucketRequest, **kwargs) → PutBucketResult

Request parameters

Parameter

Type

Description

request

PutBucketRequest

The request parameters. For more information, see PutBucketRequest

Return values

Type

Description

PutBucketResult

The return value. For more information, see PutBucketResult

For the complete method definition, see put_bucket.

Sample code

You can use the following code to create a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="put bucket sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
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 arguments.

    # Load credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK and set the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    # Set the region in the configurations.
    cfg.region = args.region
    # If the endpoint argument is provided, set the endpoint in the configurations.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the configured information to create an OSS client.
    client = oss.Client(cfg)

    # Execute the request to create a bucket and set the storage class to Standard.
    result = client.put_bucket(oss.PutBucketRequest(
        bucket=args.bucket,
        create_bucket_configuration=oss.CreateBucketConfiguration(
            storage_class='Standard'
        )
    ))
    # Print the status code and request ID of the result to check whether the request is successful.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )


if __name__ == "__main__":
    main()  # The entry point of the script. The main function is called when the file is run directly.

References