下载文件到本地
将指定存储桶中的文件(Object)下载到本地文件。代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:
//指定目标文件所存在的桶以及目标文件(Object)名称
std::string BucketName = "yourBucketName";
std::string ObjectName = "yourObjectName";
//设置下载到本地后的文件名,需包含文件路径,比如C:\\User\\Name\Desktop\\abc.txt
std::string FileNametoSave = "yourFileName";
//初始化资源
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
//获取文件到本地文件
GetObjectRequest request(BucketName, ObjectName);
request.setResponseStreamFactory([=]()
{return std::make_shared<std::fstream>(FileNametoSave, std::ios_base::out | std::ios_base::in | std::ios_base::trunc | std::ios_base::binary); });
auto outcome = client.GetObject(request);
//处理结果
if (outcome.isSuccess()) {
std::cout << "GetObjectToFile success, ContentLength:"<<outcome.result().Metadata().ContentLength() << std::endl;
}
//异常处理
else {
std::cout << "GetObjectToFile fail" << std::endl;
std::cout << "error code:" << outcome.error().Code() << std::endl;
std::cout << "error message:" << outcome.error().Message() << std::endl;
std::cout << "requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
//释放资源
ShutdownSdk();