The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

Checking Whether a Bucket Exists

Last updated: 2026-05-12 09:26:19

Introduction

This document provides an overview of the API for checking whether a bucket exists and SDK sample code.

Related Examples

Feature Name
Description
Example code
Retrieving Buckets and Their Permissions
Queries the bucket existence and access permission

Prerequisites: Initializing a COS Service Instance

public class BucketModel { private CosXml cosXml; // Sets the service user as a data member // Initializes the COS service instance private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // Sets the default region. For COS region abbreviations, refer to https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // TencentCloud API key SecretId. To obtain API keys, refer to https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // TencentCloud API key SecretKey. To obtain API keys, refer to https://console.cloud.tencent.com/cam/capi long durationSecond = 600; // Signature validity period for each request, in seconds QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }

Use Case

Checking Whether a Bucket Exists

DoesBucketExist is a simplified HEAD Bucket API provided by the .NET SDK, which is used solely to check whether a bucket exists and returns a Boolean value. To obtain further bucket details, use the Search Bucket API.
public void DoesBucketExist() { try { // Bucket name. The format must be BucketName-APPID. For information on how to obtain the APPID, refer to https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; DoesBucketExistRequest request = new DoesBucketExistRequest(bucket); // Executes the request bool existState = cosXml.DoesBucketExist(request); // Request succeeded Console.WriteLine("bucket exist state is: " + existState); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }