查询存储桶列表

最近更新时间:2024-09-14 16:51:01

我的收藏

简介

本文档提供关于查询存储桶列表的 API 概览以及 SDK 示例代码。

相关示例

功能名称
描述
示例代码
查询存储桶列表
查询指定账号下所有的存储桶列表

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

public class DownloadObject { 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); } }


使用案例

查询存储桶列表

public void GetService() { try { GetServiceRequest request = new GetServiceRequest(); //执行请求 GetServiceResult result = cosXml.GetService(request); //得到所有的 buckets List<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets; foreach (var bucket in allBuckets) { Console.WriteLine(bucket.name); } } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

API 操作

关于查询存储桶列表的 API 接口说明,请参见 GET Service (List Buckets) 文档。