A bucket is a container for objects in Object Storage Service (OSS). Files that you upload are stored as objects in buckets. Use the OSS SDK for C# V2 to create a bucket programmatically.
Prerequisites
Before you begin, make sure that you have:
An Alibaba Cloud account with the required permissions
The OSS SDK for C# V2 installed
An AccessKey ID and AccessKey secret stored in environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)
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 policies.
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and the public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, 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.
Sample code
The following code creates a bucket with the default Standard storage class.
using OSS = AlibabaCloud.OSS.V2;
// Specify the region. Example: cn-hangzhou for China (Hangzhou).
var region = "cn-hangzhou";
// Specify the bucket name.
var bucket = "your bucket name";
// (Optional) Specify the endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = null as string;
// Load default SDK configuration. Credentials are read from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Use environment variables for authentication (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region.
cfg.Region = region;
// Override the default endpoint if specified.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create the OSS client.
using var client = new OSS.Client(cfg);
// Create the bucket. The default storage class is Standard.
var result = await client.PutBucketAsync(new OSS.Models.PutBucketRequest()
{
Bucket = bucket
});
// Print the result.
Console.WriteLine("PutBucket done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response Headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));Code walkthrough
| Step | Code | Description |
|---|---|---|
| 1. Set up the SDK alias | using OSS = AlibabaCloud.OSS.V2; | Creates a namespace alias for convenience |
| 2. Configure variables | region, bucket, endpoint | region and bucket are required. endpoint is optional and overrides the default |
| 3. Load configuration | OSS.Configuration.LoadDefault() | Loads default settings and reads credentials from environment variables |
| 4. Set credentials provider | EnvironmentVariableCredentialsProvider() | Reads OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET from environment variables |
| 5. Create the client | new OSS.Client(cfg) | Initializes the OSS client with the configuration |
| 6. Create the bucket | client.PutBucketAsync(...) | Sends the PutBucket request. The default storage class is Standard |
| 7. Verify the result | result.StatusCode, result.RequestId | StatusCode returns the HTTP status code. RequestId is useful for troubleshooting with Alibaba Cloud support |