上传本地文件

将本地文件上传到存储桶。代码中需要引用"inspurcloud/oss/OssClient.h"类库、"fstream"类库,使用InspurCloud::OSS命名空间,并初始化账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:

        //指定上传到的存储桶以及上传后文件(Object)的名字
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";

        //初始化资源
        InitializeSdk();
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        //"yourLocalFilename"需要包含本地路径,例如C:\\Users\\NAME\\Desktop\\abc.txt
        std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("yourLocalFilename", std::ios::in | std::ios::binary);
        PutObjectRequest request(BucketName, ObjectName, content);
        auto outcome = client.PutObject(request);

        //处理结果
        if (!outcome.isSuccess()) {
            std::cout << "PutObject 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;
        }
        else{
            std::cout << "PutObject Success!" << std::endl;
        }

        //释放资源
        ShutdownSdk();