删除文件
更新时间:2022-10-08
删除文件
删除单个文件
可参考如下代码删除了一个Object:
int deleteObject(Client& client,const std::string bucketName, const std::string objectKey) {
// 删除Object
return client.delete_object(bucketName, objectKey); //指定要删除的Object所在Bucket名称和该Object名称
}
删除多个文件
删除多个文件deleteMultiObject
使用并发接口,提高请求数的吞吐,代码如下:
int deleteMultiObject(Client& client, const& std::string bucketName, const std::vector<std::string>& objects) {
//批量删除object
std::vector<BceRequestContext> ctx(objects.size());
int i = 0;
for (const std::string& objectKey : objects) {
//构造上下文信息
ctx[i].request = new DeleteObjectRequest(bucketName, objectKey);
ctx[i].response = new DeleteObjectResponse;
ctx[i].is_own = true;//自动析构request和response
++i;
}
//并发请求
int ret = client.send_request((int)ctx.size(), &ctx.front());
if (ret != 0) return ret;
//处理response
for (size_t i = 0; i < ctx.size(); ++i) {
DeleteObjectRequest* request = (DeleteObjectRequest*)ctx[i].request;
DeleteObjectResponse* response = (DeleteObjectResponse*)ctx[i].response;
if (response->is_fail()) {//有请求删除失败,处理这种情况
std::cout << "delete objectKey=" << request->object_name() << " failed"
<< " reason: " << response->error().message();
ret = RET_SERVICE_ERROR;
}
}
return ret;
}