简介
本文档提供关于清单的 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 inventory 用于在存储桶中创建清单任务。
public void PutBucketInventory() { try { string inventoryId = "aInventoryId"; // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; PutBucketInventoryRequest putRequest = new PutBucketInventoryRequest(bucket, inventoryId); putRequest.SetDestination("CSV", "100000000001", "examplebucket-1250000000", "ap-guangzhou", "list1"); putRequest.IsEnable(true); // 清单任务周期,枚举值:Daily、Weekly putRequest.SetScheduleFrequency("Daily"); // 是否在清单中包含对象版本,枚举值:All、Current putRequest.SetIncludedObjectVersions("All"); //执行请求 PutBucketInventoryResult putResult = cosXml.PutBucketInventory(putRequest); //请求成功 Console.WriteLine(putResult.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
错误码说明
该请求可能会发生的一些常见的特殊错误如下:
错误码 | 描述 | 状态码 |
InvalidArgument | 不合法的参数值 | HTTP 400 Bad Request |
TooManyConfigurations | 清单数量已经达到1000条的上限 | HTTP 400 Bad Request |
AccessDenied | 未授权的访问。您可能不具备访问该存储桶的权限 | HTTP 403 Forbidden |
查询清单任务
GET Bucket inventory 用于查询存储桶中用户的清单任务信息。
public void GetBucketInventory() { try { string inventoryId = "aInventoryId"; // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketInventoryRequest getRequest = new GetBucketInventoryRequest(bucket); getRequest.SetInventoryId(inventoryId); GetBucketInventoryResult result = cosXml.GetBucketInventory(getRequest); InventoryConfiguration configuration = result.inventoryConfiguration; 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 inventory 用于删除存储桶中指定的清单任务。
public void DeleteBucketInventory() { try { string inventoryId = "aInventoryId"; // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; DeleteBucketInventoryRequest deleteRequest = new DeleteBucketInventoryRequest(bucket); deleteRequest.SetInventoryId(inventoryId); DeleteBucketInventoryResult deleteResult = cosXml.DeleteBucketInventory(deleteRequest); //请求成功 Console.WriteLine(deleteResult.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
API 操作
关于设置清单任务的 API 接口说明,请参见 PUT Bucket inventory 文档。
关于查询清单任务的 API 接口说明,请参见 GET Bucket inventory 文档。
关于删除清单任务的 API 接口说明,请参见 DELETE Bucket inventory 文档。