Check whether a bucket exists (C# SDK V1)

更新时间:
复制 MD 格式

DoesBucketExist() returns true if the bucket exists, or false if it does not. Use this method before operations that depend on the bucket being present — for example, to avoid redundant create calls or to branch your logic based on bucket state. This is preferable to attempting an operation and catching the resulting exception.

Prerequisites

Before you begin, ensure that you have:

  • The oss:GetBucketAcl permission on the target bucket. For details, see Attach a custom policy to a RAM user.

  • An OSSClient instance initialized with a valid endpoint and credentials. For initialization options including custom domain names and Security Token Service (STS), see Initialization.

Note: The examples below use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.

Check whether a bucket exists

The following example calls DoesBucketExist() and branches on the result.

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

namespace Samples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Replace with the endpoint for the region where your bucket is located.
            // Example: https://oss-cn-hangzhou.aliyuncs.com
            var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

            // Load credentials from environment variables.
            // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
            var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
            var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");

            var bucketName = "examplebucket";

            // Set the region to match your endpoint. Example: cn-hangzhou
            const string region = "cn-hangzhou";

            // Create a ClientConfiguration instance and set the signature version to V4.
            var conf = new ClientConfiguration();
            conf.SignatureVersion = SignatureVersion.V4;

            var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
            client.SetRegion(region);

            try
            {
                // Returns true if the bucket exists; false if it does not exist.
                var exists = client.DoesBucketExist(bucketName);

                if (exists)
                {
                    Console.WriteLine("Bucket '{0}' exists.", bucketName);
                }
                else
                {
                    Console.WriteLine("Bucket '{0}' does not exist.", bucketName);
                    // Add bucket creation logic here if needed.
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to check bucket existence: {0}", ex.Message);
            }
        }
    }
}

For the complete sample, see DoesBucketExistSample.cs on GitHub.