All Products
Search
Document Center

Object Storage Service:Create a bucket (C# SDK V2)

Last Updated:Aug 06, 2025

A bucket is a container for objects. Files that you upload are stored as objects in buckets. This topic describes how to create a bucket using the C# 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.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. The sample code uses the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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.

Sample code

You can use the following code to create a bucket.

using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou"; // Required. Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
var bucket = "your bucket name";  // Required. Specify the name of the bucket to be created.
var endpoint = null as string;  // Optional. Specify the domain name used to access OSS. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.

// Load the default configurations of OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configurations.
cfg.Region = region;
// If an endpoint is specified, overwrite the default endpoint.
if(endpoint != null)
{
    cfg.Endpoint = endpoint;
}

// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);

// Call the PutBucketAsync method to create a new bucket. The storage class is Standard.
var result = await client.PutBucketAsync(new OSS.Models.PutBucketRequest()
{
    Bucket = bucket
});

// Print the creation result.
Console.WriteLine("PutBucket done");  // The operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}");  // RequestId, which is used by Alibaba Cloud to troubleshoot issues.
Console.WriteLine("Response Headers:");  // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.

References

For the complete sample code to create a bucket, see PutBucket.cs.