API | 操作名 | 操作描述 |
查询存储桶列表 | 查询指定账号下所有的存储桶列表 |
using COSXML.Model.Tag;using COSXML.Model.Service;using COSXML.Auth;using System;using COSXML;using System.Collections.Generic;namespace COSSnippet{public class GetServiceModel {private CosXml cosXml;GetServiceModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}/// 获取存储桶列表public void GetService(){//.cssg-snippet-body-start:[get-service]try{GetServiceRequest request = new GetServiceRequest();//执行请求GetServiceResult result = cosXml.GetService(request);//得到所有的 bucketsList<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets;}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}//.cssg-snippet-body-end}/// 获取地域的存储桶列表public void GetRegionalService(){//.cssg-snippet-body-start:[get-regional-service]try{GetServiceRequest request = new GetServiceRequest();string region = "ap-guangzhou";request.host = $"cos.{region}.myqcloud.com";//执行请求GetServiceResult result = cosXml.GetService(request);//得到所有的 bucketsList<ListAllMyBuckets.Bucket> allBuckets = result.listAllMyBuckets.buckets;}catch (COSXML.CosException.CosClientException clientEx){//请求失败Console.WriteLine("CosClientException: " + clientEx);}catch (COSXML.CosException.CosServerException serverEx){//请求失败Console.WriteLine("CosServerException: " + serverEx.GetInfo());}//.cssg-snippet-body-end}// .cssg-methods-pragmastatic void Main(string[] args){GetServiceModel m = new GetServiceModel();/// 获取存储桶列表m.GetService();/// 获取地域的存储桶列表m.GetRegionalService();// .cssg-methods-pragma}}}
本页内容是否解决了您的问题?