简介
本文介绍对象存储 COS 通过 .NET(C#) SDK 实现列出对象功能的示例代码和描述。
注意事项
若您要使用列出对象功能,需要先具有目标桶的读取权限:在您进行 授权策略 时,action 需要设置为
cos:GetBucket
,更多授权请参见 支持CAM的业务接口。若您要使用列出对象历史版本功能,需要先具有目标桶的读取权限:在您进行 授权策略 时,action 需要设置为
cos:GetBucketObjectVersions
,更多授权请参见 支持CAM的业务接口。相关示例
功能名称 | 描述 | 示例代码 |
列出对象 | 提供了查询存储桶下的部分或者全部对象、查询存储桶下的部分或者全部对象及其历史版本信息的能力。 |
前期准备:初始化 COS 服务实例
public class ObjectModel { 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 GetBucketFirstPage() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketRequest request = new GetBucketRequest(bucket); //执行请求 GetBucketResult result = cosXml.GetBucket(request); //bucket的相关信息 ListBucket info = result.listBucket; if (info.isTruncated) { // 数据被截断,记录下数据下标 this.nextMarker = info.nextMarker; } Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
请求下一页数据
public void GetBucketNextPage() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketRequest request = new GetBucketRequest(bucket); //上一次拉取数据的下标 request.SetMarker(this.nextMarker); //执行请求 GetBucketResult result = cosXml.GetBucket(request); //bucket的相关信息 ListBucket info = result.listBucket; Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
获取对象列表与子目录
public void GetBucketWithDelimiter() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketRequest request = new GetBucketRequest(bucket); //获取 a/ 下的对象以及子目录 request.SetPrefix("dir/"); request.SetDelimiter("/"); //执行请求 GetBucketResult result = cosXml.GetBucket(request); //bucket的相关信息 ListBucket info = result.listBucket; // 对象列表 List<ListBucket.Contents> objects = info.contentsList; // 子目录列表 List<ListBucket.CommonPrefixes> subDirs = info.commonPrefixesList; Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
获取对象历史版本列表第一页数据
public void ListObjectsVersioning() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket); //执行请求 ListBucketVersionsResult result = cosXml.ListBucketVersions(request);//请求结果状态 Console.WriteLine(result.GetResultInfo()); //bucket的相关信息 ListBucketVersions info = result.listBucketVersions;Console.WriteLine(info); List<ListBucketVersions.Version> objects = info.objectVersionList; List<ListBucketVersions.CommonPrefixes> prefixes = info.commonPrefixesList; if (info.isTruncated) { // 数据被截断,记录下数据下标 this.keyMarker = info.nextKeyMarker; this.versionIdMarker = info.nextVersionIdMarker; }Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
获取对象历史版本列表下一页数据
public void ListObjectsVersioningNextPage() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; ListBucketVersionsRequest request = new ListBucketVersionsRequest(bucket); // 上一页的数据结束下标 request.SetKeyMarker(this.keyMarker); request.SetVersionIdMarker(this.versionIdMarker); //执行请求 ListBucketVersionsResult result = cosXml.ListBucketVersions(request);//请求结果状态Console.WriteLine(result.GetResultInfo()); ListBucketVersions info = result.listBucketVersions;//返回数据Console.WriteLine(info.GetInfo()); if (info.isTruncated) { // 数据被截断,记录下数据下标 this.keyMarker = info.nextKeyMarker; this.versionIdMarker = info.nextVersionIdMarker; } } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
API 操作
关于列出对象的 API 接口说明,请参见 List Objects 文档。
关于列出对象历史版本的 API 接口说明,请参见 List Object versions 文档。