本文介绍如何将OSS文件下载到本地内存。
以下代码用于把指定的OSS文件下载到本地内存:
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /*初始化OSS账号信息。*/ /*阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/ std::string AccessKeyId = "yourAccessKeyId"; std::string AccessKeySecret = "yourAccessKeySecret"; /*填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/ std::string Endpoint = "yourEndpoint"; /*填写Bucket名称。*/ std::string BucketName = "yourBucketName"; /*填写不包含Bucket名称在内的Object的完整路径,例如desrfolder/exampleobject.txt。*/ std::string ObjectName = "yourObjectName"; /*初始化网络等资源。*/ InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf); /*获取文件到本地内存。*/ GetObjectRequest request(BucketName, ObjectName); auto outcome = client.GetObject(request); if (outcome.isSuccess()) { std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl; /*通过read接口读取数据。*/ auto& stream = outcome.result().Content(); char buffer[256]; while (stream->good()) { stream->read(buffer, 256); auto count = stream->gcount(); /*根据实际情况处理数据。*/ } } else { /*异常处理。*/ std::cout << "getObjectToBuffer fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; ShutdownSdk(); return -1; } /*释放网络等资源。*/ ShutdownSdk(); return 0; }