范围下载文件
打印文件(Object)指定范围的内容到内存。代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:
//指定目标文件所存在的桶以及目标文件(Object)名称
std::string BucketName = "yourBucketName";
std::string ObjectName = "yourObjectName";
//初始化资源
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
//获取文件到内存
GetObjectRequest request(BucketName, ObjectName);
request.setRange(0, 1); //指定字节范围
auto outcome = client.GetObject(request);
//处理结果
if (outcome.isSuccess()) {
std::cout << "getObjectToBuffer success ,Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
std::cout << "Content:" << std::endl;
std::string content;
while (std::getline(*(outcome.result().Content()), content)) {
std::cout << content << std::endl << std::endl;
}
}
//异常处理
else {
std::cout << "getObjectToBuffer 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();