Buckets are containers that store objects in OSS. Create a bucket by using OSS SDK for C# 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 policies or Bucket Policy.
|
API |
Action |
Description |
|
PutBucket |
|
Creates a bucket. |
|
|
After creating a bucket, this permission is required to modify the bucket ACL. |
Precautions
-
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint listed in 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; // 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
Complete sample code: PutBucket.cs.