有奖捉虫:云通信与企业服务文档专题,速来> HOT

简介

本文档提供关于查询存储桶列表的 API 概览以及 SDK 示例代码。
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://cloud.tencent.com/document/product/436/6224
.Build();

string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
string secretKey = "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()
{
//.cssg-snippet-body-start:[get-service]
try
{
GetServiceRequest request = new GetServiceRequest();
//执行请求
GetServiceResult result = cosXml.GetService(request);
//得到所有的 buckets
List<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);
//得到所有的 buckets
List<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-pragma

static void Main(string[] args)
{
GetServiceModel m = new GetServiceModel();

/// 获取存储桶列表
m.GetService();
/// 获取地域的存储桶列表
m.GetRegionalService();
// .cssg-methods-pragma
}
}
}

说明
更多完整示例,请前往 GitHub 查看。