Feature Overview
This document provides an overview of APIs and SDK code samples for creating a bucket.
Note
We recommend users to use temporary keys when calling the SDK to enhance security through temporary authorization. When applying for temporary keys, please follow the principle of least privilege to prevent leakage of resources beyond the target bucket or object.
If you must use a permanent key, we recommend you follow the Notes on Principle of Least Privilege to limit the scope of permission on the permanent key.
API | Operation | Description |
Create a bucket. | Creates a bucket under a specified account |
SDK API References
Create a bucket.
Note
This API is used to create a bucket under the specified account. You can create multiple buckets under the same user account. The maximum number is 200 (regardless of region). There is no limit to the number of objects in the bucket. Bucket creation is a low-frequency operation. We recommended you create a bucket in the console and perform object operations in the SDK.
Sample code
using COSXML.Common;using COSXML.Model.Bucket;using COSXML.Auth;using System;using COSXML;namespace COSSnippet{public class PutBucketModel {private CosXml cosXml;PutBucketModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // Set the default region. For abbreviations of COS regions, please visit https://cloud.tencent.com/document/product/436/6224.Build();string secretId = "SECRET_ID"; // TencentCloud API key SecretId, to obtain an API key, please refer to https://console.cloud.tencent.com/cam/capistring secretKey = "SECRET_KEY"; // SecretKey of your TencentCloud API key, to obtain an API key, please refer to https://console.cloud.tencent.com/cam/capilong durationSecond = 600; // Validity period of each request signature, in seconds.QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// Create a bucketpublic void PutBucket(){//.cssg-snippet-body-start:[put-bucket]try{// Bucket name format must be bucketname-APPID, where APPID can be obtained from https://console.cloud.tencent.com/developerstring bucket = "examplebucket-1250000000";PutBucketRequest request = new PutBucketRequest(bucket);// Execute the requestPutBucketResult result = cosXml.PutBucket(request);//Request successfulConsole.WriteLine(result.GetResultInfo());}catch (COSXML.CosException.CosClientException clientEx){// Request failedConsole.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){// Request failedConsole.WriteLine("CosServerException: " + serverEx.GetInfo());}//.cssg-snippet-body-end}// .cssg-methods-pragmastatic void Main(string[] args){PutBucketModel m = new PutBucketModel();/// Create a bucketm.PutBucket();// .cssg-methods-pragma}}}
Note