Create a bucket (C# SDK V1)

Updated at:

A bucket is a container for storing objects. All objects must be stored in a bucket. This topic describes how to create a bucket.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization (C# SDK V1).

  • In this example, a bucket with a region attribute is created. If you want to use the following code to create a bucket without a region attribute, you must replace the endpoint with https://oss-rg-china-mainland.aliyuncs.com. In addition, a bucket without a region attribute supports only the Standard storage class and only locally redundant storage (LRS) for data redundancy, and does not support resource group configuration. For more information about buckets without a region attribute, see Bucket region 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.

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.

Sample code

The following code provides an example of how to create a bucket named examplebucket.

using Aliyun.OSS;
using Aliyun.OSS.Common;

// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";

// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();

// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
// Create a bucket.
try
    {
        var request = new CreateBucketRequest(bucketName);
        // Set the access control list (ACL) to public-read. The default ACL is private.
        request.ACL = CannedAccessControlList.PublicRead;
        // Set the data disaster recovery type to zone-redundant storage.
        request.DataRedundancyType = DataRedundancyType.ZRS;
        client.CreateBucket(request);
        Console.WriteLine("Create bucket succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Create bucket failed. {0}", ex.Message);
    }

References

  • For the complete sample code that is used to create a bucket, see GitHub.

  • For more information about the API operation that is used to create a bucket, see PutBucket.