Use IsBucketExistAsync to check whether a bucket exists. The method returns true if the bucket exists, or false if it does not exist.
Prerequisites
Before you begin, make sure you have:
The
oss:GetBucketAclpermission on the target bucket. For details, see Grant custom permissions to a RAM userYour AccessKey ID and AccessKey Secret stored in environment variables
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET
Sample code
The following example uses the China (Hangzhou) region (cn-hangzhou) and a public endpoint. To access OSS from other Alibaba Cloud services in the same region, replace the endpoint with an internal endpoint. For region-to-endpoint mappings, see OSS regions and endpoints.
using OSS = AlibabaCloud.OSS.V2;
var region = "cn-hangzhou";
var bucket = "your bucket name";
var endpoint = null as string; // Optional. For example: https://oss-cn-hangzhou.aliyuncs.com
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
cfg.Endpoint = endpoint;
}
using var client = new OSS.Client(cfg);
var result = await client.IsBucketExistAsync(bucket);
Console.WriteLine("IsBucketExist done");
Console.WriteLine($"result: {result}");References
For the complete sample code, see is_bucket_exist.cs.