递归火山软件开发平台
标题:
求封装一下阿里OSS操作,或者有详细封装教程吗
[打印本页]
作者:
是 非
时间:
2021-2-15 17:13
标题:
求封装一下阿里OSS操作,或者有详细封装教程吗
#
include
<alibabacloud/oss/OssClient.h>
#include <fstream>
using
namespace
AlibabaCloud::OSS;int main(void){
/* 初始化OSS账号信息 */
std
::
string
AccessKeyId =
"yourAccessKeyId"
;
std
::
string
AccessKeySecret =
"yourAccessKeySecret"
;
std
::
string
Endpoint =
"yourEndpoint"
;
std
::
string
BucketName =
"yourBucketName"
;
/* yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg*/
std
::
string
ObjectName =
"yourObjectName"
;
/* 初始化网络等资源 */
InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
std
::
shared_ptr
<
std
::iostream> content =
std
::make_shared<
std
::fstream>(
"yourLocalFilename"
,
std
::ios::in |
std
::ios::binary); PutObjectRequest request(BucketName, ObjectName, content);
/*(可选)请参见如下示例设置存储类型及访问权限ACL*/
//request.MetaData().addHeader("x-oss-object-acl", "private");
//request.MetaData().addHeader("x-oss-storage-class", "Standard");
auto
outcome = client.PutObject(request);
if
(!outcome.isSuccess()) {
/* 异常处理 */
std
::
cout
<<
"PutObject fail"
<<
",code:"
<< outcome.error().Code() <<
",message:"
<< outcome.error().Message() <<
",requestId:"
<< outcome.error().RequestId() <<
std
::
endl
; ShutdownSdk();
return
-1
; }
/* 释放网络等资源 */
ShutdownSdk();
return
0
;}
欢迎光临 递归火山软件开发平台 (https://bbs.voldp.com/)
Powered by Discuz! X3.4