控制台指南

最佳实践

开发者指南

数据湖存储

API 文档

SDK 文档

文档捉虫大赛火热进行中,好礼多多> HOT

简介

本文档提供关于清单的 API 概览以及 SDK 示例代码。

API 操作名 操作描述
PUT Bucket inventory 设置清单任务 设置存储桶的清单任务
GET Bucket inventory 查询清单任务 查询存储桶的清单任务
LIST Bucket inventory 列出清单任务 列出存储桶的清单任务
DELETE Bucket inventory 删除清单任务 删除存储桶的清单任务

设置清单任务

功能说明

PUT Bucket inventory 用于在存储桶中创建清单任务。

方法原型

CosResult BucketOp::PutBucketInventory(const PutBucketInventoryReq& req, PutBucketInventoryResp* resp);

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";
qcloud_cos::PutBucketInventoryReq req(bucket_name);
qcloud_cos::PutBucketInventoryResp resp;
// 设置清单任务
req.SetId("list");
COSBucketDestination destination;
destination.SetFormat("CSV");
destination.SetAccountId("100010974959");
destination.SetBucket("qcs::cos:ap-guangzhou::loggtest-1234567890");
destination.SetPrefix("/");
destination.SetEncryption(true);
OptionalFields fields;
fields.SetIsSize(true);
fields.SetIsLastModified(true);
fields.SetIsStorageClass(true);
fields.SetIsMultipartUploaded(true);
fields.SetIsReplicationStatus(true);
fields.SetIsEtag(true);
Inventory inventory;
inventory.SetIsEnable(true);
inventory.SetIncludedObjectVersions("All");
inventory.SetFilter("/");
inventory.SetId(req.GetId());
inventory.SetFrequency("Daily");
inventory.SetCOSBucketDestination(destination);
inventory.SetOptionalFields(fields);
req.SetInventory(inventory);
qcloud_cos::CosResult result = cos.PutBucketInventory(req, &resp);
if (result.IsSucc()) {
  // 请求成功
} else {
  // 请求失败,可以调用 CosResult 的成员函数输出错误信息,如 requestID 等
}

参数说明

参数 参数描述 类型 是否必填
req PutBucketInventory 操作的请求 PutBucketInventoryReq
resp PutBucketInventory 操作的响应 PutBucketInventoryResp

PutBucketInventoryReq 提供以下成员函数:

// 设置清单规则
void SetInventory(Inventory& inventory);

该请求涉及到的类定义如下:

// 清单配置参数
class Inventory {
  void SetId(const std::string& id) // 设置清单的名称
  void SetIsEnable(bool is_enabled) // 清单是否启用    
  void SetIncludedObjectVersions(const std::string& included_objectversions) // 是否在清单中包含对象版本:
  void SetFilter(const std::string& filter) // 筛选待分析对象(前缀)    
  void SetCOSBucketDestination(const COSBucketDestination& destination) // 设置清单结果导出后存放的存储桶信息
  void SetOptionalFields(const OptionalFields& fields) // 设置清单结果中应包含的分析项目
  void SetFrequency(const std::string& frequency) // 设置清单任务的周期,支持 Daily 和 Weekly
}
// 清单结果中应包含的分析项目
class OptionalFields {
  void SetIsSize(const bool size); // 设置是否需要文件大小
  void SetIsLastModified(const bool last_modified) // 设置是否需要文件修改时间
  void SetIsStorageClass(const bool storage_class) // 设置是否需要存储类型
  void SetIsMultipartUploaded(const bool ismultipart_uploaded) // 设置是否需要分块上传    
  void SetIsReplicationStatus(const bool replication_status) // 设置是否需要跨区域复制状态
}
// 清单结果导出后存放的存储桶信息
class COSBucketDestination {
public:
  void SetFormat(const std::string& format) // 设置清单生成文件格式,目前支持 CSV 格式    
  void SetAccountId(const std::string& accountId) // 存储桶的所有者 ID  
  void SetBucket(const std::string& bucket) // 设置存储桶完整 ID
  void SetPrefix(const std::string& prefix) // 设置清单分析结果的存储桶名    
  void SetEncryption(const bool encryption) // 设置清单结果是否加密    

错误码说明

该请求可能会发生的一些常见的特殊错误如下:

错误码 描述 状态码
InvalidArgument 不合法的参数值 HTTP 400 Bad Request
TooManyConfigurations 清单数量已经达到1000条的上限 HTTP 400 Bad Request
AccessDenied 未授权的访问。您可能不具备访问该存储桶的权限 HTTP 403 Forbidden

查询清单任务

功能说明

GET Bucket inventory 用于查询存储桶中用户的清单任务信息。

方法原型

CosResult CosAPI::GetBucketInventory(const GetBucketInventoryReq& request, GetBucketInventoryResp* response) {                                      

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";
qcloud_cos::GetBucketInventoryReq req(bucket_name);
qcloud_cos::GetBucketInventoryResp resp;
// 设置清单任务
req.SetId("list");
qcloud_cos::CosResult result = cos.GetBucketInventoryResp(req, &resp);
if (result.IsSucc()) {
   // 请求成功,获取清单信息
   //const Inventory inventory = resp.GetInventory();
   //std::cout << "inventory isenabled:" << inventory.GetIsEnable() << std::endl;
   //std::cout << "inventory IncludedObjectVersions:" << inventory.GetIncludedObjectVersions() << std::endl;
   //std::cout << "inventory filter:" << inventory.GetFilter() << std::endl;
   //std::cout << "inventory id:" << inventory.GetId() << std::endl;
   //std::cout << "inventory Frequency:" << inventory.GetFrequency() << std::endl;
    //std:: cout << "===================================" << std::endl;
   //COSBucketDestination destination = inventory.GetCOSBucketDestination();
   //std::cout << "destination Format:" << destination.GetFormat() << std::endl;
   //std::cout << "destination AccountID:" << destination.GetAccountId() << std::endl;
   //std::cout << "destination Bucket:" << destination.GetBucket() << std::endl;
   //std::cout << "destination Encryption:" << destination.GetEncryption() << std::endl;
    //std:: cout << "===================================" << std::endl;
   //    
   //OptionalFields fields = inventory.GetOptionalFields();
    //std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
   //std::cout << "fields LastModified:" << fields.GetIsLastModified() << std::endl;
   //std::cout << "fields StorageClass:" << fields.GetIsStorageClass() << std::endl;
   //std::cout << "fields Region:" << fields.GetIsMultipartUploaded() << std::endl;
   //std::cout << "fields ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
   //std::cout << "fields Tag:" <<     fields.GetIsETag() << std::endl;
} else {
   // 请求失败,可以调用 CosResult 的成员函数输出错误信息,如 requestID 等
}

参数说明

参数 参数描述 类型 是否必填
req GetBucketInventory 操作的请求 GetBucketInventoryReq
resp GetBucketInventory 操作的响应 GetBucketInventoryResp

GetBucketInventoryResp 提供以下成员函数:

// 获取清单规则
const Inventory& GetInventory() const;

查询所有清单

功能说明

List Bucket Inventory Configurations 用于请求返回一个存储桶中的所有清单任务。

方法原型

CosResult CosAPI::ListBucketInventoryConfigurations(const ListBucketInventoryConfigurationsReq& request, ListBucketInventoryConfigurationsResp* response) {                                      

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";
qcloud_cos::ListBucketInventoryConfigurationsReq req(bucket_name);
qcloud_cos::ListBucketInventoryConfigurationsResp resp;
qcloud_cos::CosResult result = cos.ListBucketInventoryConfigurations(req, &resp);
if (result.IsSucc()) {
  // 请求成功,获取清单信息
 // std::cout << "===================ListBucketInventoryConfigurations=====================" << std::endl;
 //     
 // std::vector<Inventory> inventory_vec = resp.GetInventory();
 //     
 // std::cout << resp.GetIsTruncated() << std::endl;
 // std::cout << resp.GetContinuationToken() << std::endl;
 // std::cout << resp.GetNextContinuationToken() << std::endl;
 //     
 // std::vector<Inventory>::iterator itr = inventory_vec.begin();
 // for(; itr != inventory_vec.end(); ++itr) {
 //         
 //    std:: cout << "==============Inventory=============================" << std::endl;
 //    std::cout << "inventory id:" << itr->GetId() << std::endl;
 //    std::cout << "inventory isenabled:" <<  itr->GetIsEnable() << std::endl;
 //    std::cout << "inventory IncludedObjectVersions:" << itr->GetIncludedObjectVersions() << std::endl;
 //    std::cout << "inventory filter:" << itr->GetFilter() << std::endl;
 //    std::cout << "inventory Frequency:" <<     itr->GetFrequency() << std::endl;
 //    std:: cout << "==============GetCOSBucketDestination==================" << std::endl;
 //    COSBucketDestination destination = itr->GetCOSBucketDestination();
 //    std::cout << "destination Format:" << destination.GetFormat() << std::endl;
 //    std::cout << "destination AccountID:" << destination.GetAccountId() << std::endl;
 //    std::cout << "destination Bucket:" << destination.GetBucket() << std::endl;
 //    std::cout << "destination Encryption:" << destination.GetEncryption() << std::endl;
 //          
 //    std:: cout << "===================OptionalFields======================" << std::endl;
 //    OptionalFields fields = itr->GetOptionalFields();
 //    std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
 //    std::cout << "fields LastModified:" << fields.GetIsLastModified() << std::endl;
 //    std::cout << "fields StorageClass:" << fields.GetIsStorageClass() << std::endl;
 //    std::cout << "fields Region:" << fields.GetIsMultipartUploaded() << std::endl;
 //    std::cout << "fields ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
 //    std::cout << "fields Tag:" <<     fields.GetIsETag() << std::endl;
 // }    
 // std::cout << "===============ListBucketInventoryConfigurations end================" << std::endl;
} else {
  // 请求失败,可以调用 CosResult 的成员函数输出错误信息,如 requestID 等
}

参数说明

参数 参数描述 类型 是否必填
req ListBucketInventoryConfigurations 操作的请求 ListBucketInventoryConfigurationsReq
resp ListBucketInventoryConfigurations 操作的响应 ListBucketInventoryConfigurationsResp

ListBucketInventoryConfigurationsReq 提供以下成员函数:

// 当 COS 响应体中 IsTruncated 为 true,且 NextContinuationToken 节点中存在参数值时,您可以将这个参数作为 continuation-token 参数值,以获取下一页的清单任务信息。
void SetContinuationToken(const std::string continuation_token); 

删除清单任务

功能说明

DELETE Bucket inventory 用于删除存储桶中指定的清单任务。

方法原型

CosResult CosAPI::DeleteBucketInventory(const DeleteBucketInventoryReq& request, DeleteBucketInventoryResp* response);                          

请求示例

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);
std::string bucket_name = "examplebucket-1250000000";
qcloud_cos::DeleteBucketInventoryReq req(bucket_name);
qcloud_cos::DeleteBucketInventoryResp resp;
qcloud_cos::CosResult result = cos.DeleteBucketInventory(req, &resp);
if (result.IsSucc()) {
  // 请求成功
} else {
  // 请求失败,可以调用 CosResult 的成员函数输出错误信息,如 requestID 等

参数说明

参数 参数描述 类型 是否必填
req DeletBucketInventory 操作的请求 DeletBucketInventoryReq
resp DeletBucketInventory 操作的响应 DeletBucketInventoryResp

DeleteBucketInventoryReq 提供以下成员函数:

void SetId(const std::string id)  // 设置要删除清单的 ID
目录