本文主要介绍如何从内存中或从本地磁盘上传文件(Object)。

从内存中上传文件

以下代码用于从内存中上传内容到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket */
    std::string BucketName = "examplebucket";
    /* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称 */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "Thank you for using Alibaba Cloud Object Storage Service!";
    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;
}

上传本地文件

以下代码用于上传本地D:\\localpath路径下的examplefile.txt到目标存储空间examplebucket中exampledir目录下的exampleobject.txt文件。

#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";
    /* 填写Bucket名称,例如examplebucket */
    std::string BucketName = "examplebucket";
    /* 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称 */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    /* 填写本地文件完整路径,例如D:\\localpath\\examplefile.txt,其中localpath为本地文件examplefile.txt所在本地路径 */
    std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("D:\\localpath\\examplefile.txt", 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;
}