This topic describes how to upload objects from memory or local disks.

Upload an object from memory

The following code provides an example on how to upload data from memory to an object named exampleobject.txt in the exampledir directory of a bucket named examplebucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket to which you want to upload the object. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object to which the uploaded data is stored. Example: exampledir/exampleobject.txt. The full path of the object cannot contain bucket names. */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* Initialize network resources. */
    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);

    /* Optional. Specify the storage class and ACL of the uploaded object. */
    //request.MetaData().addHeader("x-oss-object-acl", "private");
    //request.MetaData().addHeader("x-oss-storage-class", "Standard");

    /* Upload the object. */
    auto outcome = client.PutObject(request);

    if (!outcome.isSuccess()) {
        /* Handle exceptions.*/
        std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Upload an object from a local disk

The following code provides an example on how to upload a file named examplefile.txt from a directory named D:\\localpath on the local disk to the exampledir directory of a bucket named examplebucket. The uploaded file is stored as an object named exampleobject.txt.

#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    // Specify the name of the bucket to which you want to upload the object. Example: examplebucket */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object to which the uploaded file is stored. Example: exampledir/exampleobject.txt. The path of the object cannot contain bucket names. */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    /* Specify the full path of the local file to upload. Example: D:\\localpath\\examplefile.txt, in which localpath indicates the local path in which the file is stored. */
    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);

    /* Optional. Specify the storage class and ACL of the uploaded object. */
    //request.MetaData().addHeader("x-oss-object-acl", "private");
    //request.MetaData().addHeader("x-oss-storage-class", "Standard");

    auto outcome = client.PutObject(request);

    if (!outcome.isSuccess()) {
            /* Handle exceptions.*/
            std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
        return 0;
}