存储桶标签

最近更新时间:2024-09-24 11:05:31

我的收藏

简介

本文档提供关于存储桶标签的 API 概览以及 SDK 示例代码。

相关示例

功能名称
描述
示例代码
设置存储桶标签
为已存在的存储桶设置标签
查询存储桶标签
查询指定存储桶下已有的存储桶标签
删除存储桶标签
删除指定的存储桶标签

前期准备:初始化 COS 服务实例

public class DataManageModel { private CosXml cosXml; //将服务用户设置成数据成员 // 初始化COS服务实例 private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }

使用案例

设置存储桶标签

PUT Bucket tagging 用于为已存在的存储桶设置标签。
public void PutBucketTagging() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; PutBucketTaggingRequest request = new PutBucketTaggingRequest(bucket); string akey = "aTagKey"; string avalue = "aTagValue"; string bkey = "bTagKey"; string bvalue = "bTagValue"; request.AddTag(akey, avalue); request.AddTag(bkey, bvalue); //执行请求 PutBucketTaggingResult result = cosXml.PutBucketTagging(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

查询存储桶标签

GET Bucket tagging 用于查询指定存储桶下已有的存储桶标签。
public void GetBucketTagging() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketTaggingRequest request = new GetBucketTaggingRequest(bucket); //执行请求 GetBucketTaggingResult result = cosXml.GetBucketTagging(request); //请求成功 if (result.tagging != null) { foreach (var tag in result.tagging.tagSet.tags) { Console.WriteLine("key {0} value {1}", tag.key, tag.value); } } Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

删除存储桶标签

DELETE Bucket tagging 用于删除指定存储桶下已有的存储桶标签。
public void DeleteBucketTagging() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; DeleteBucketTaggingRequest request = new DeleteBucketTaggingRequest(bucket); //执行请求 DeleteBucketTaggingResult result = cosXml.DeleteBucketTagging(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

API 操作

关于设置存储桶标签的 API 接口说明,请参见 PUT Bucket tagging 文档。
关于查询存储桶标签的 API 接口说明,请参见 GET Bucket tagging 文档。
关于删除存储桶标签的 API 接口说明,请参见 DELETE Bucket tagging 文档。