追加上传
使用内存中资源对文件(Object)追加上传。代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:
//指定上传到的存储桶以及需要追加的文件(Object) std::string BucketName = "yourBucketName"; std::string ObjectName = "yourObjectName"; //初始化资源 InitializeSdk(); ClientConfiguration conf; OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf); auto meta = ObjectMetaData(); meta.setContentType("text/plain"); //第一次追加的位置是0,返回值为下一次追加的位置,后续追加的位置是追加前文件的长度。 std::shared_ptr<std::iostream> content1 = std::make_shared<std::stringstream>(); *content1 <<"Welcome to Inspur Cloud Object Storage Service(first)!"; AppendObjectRequest request(BucketName, ObjectName, content1, meta); request.setPosition(0L); //第一次追加文件 auto result = client.AppendObject(request); //处理结果 if (!result.isSuccess()) { std::cout << "AppendObject fail" << std::endl; std::cout << "error code:" << result.error().Code() << std::endl; std::cout << "error message:" << result.error().Message() << std::endl; std::cout << "requestId:" << result.error().RequestId() << std::endl; ShutdownSdk(); return -1; } else{ std::cout << "AppendObject success!" << std::endl; } std::shared_ptr<std::iostream> content2 = std::make_shared<std::stringstream>(); *content2 <<"Welcome to Inspur Cloud Object Storage Service(second)!"; auto position = result.result().Length(); AppendObjectRequest appendObjectRequest(BucketName, ObjectName, content2); appendObjectRequest.setPosition(position); //第二次追加文件 auto outcome = client.AppendObject(appendObjectRequest); //处理结果 if (!result.isSuccess()) { std::cout << "AppendObject 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 << "AppendObject success!" << std::endl; } //释放资源 ShutdownSdk();
使用本地文件对文件(Object)追加上传。代码中需要引用"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); auto out = client.HeadObject(BucketName, ObjectName); auto length = out.result().ContentLength(); auto meta = ObjectMetaData(); meta.setContentType("text/plain"); //"yourLocalFilename"需要包含本地路径,例如C:\\Users\\NAME\\Desktop\\abc.txt auto fileToUpload = "yourLocalFilename"; auto result = client.AppendObject(BucketName, ObjectName, fileToUpload, length); //处理结果 if (!result.isSuccess()) { std::cout << "AppendObject fail" << std::endl; std::cout << "error code:" << result.error().Code() << std::endl; std::cout << "error message:" << result.error().Message() << std::endl; std::cout << "requestId:" << result.error().RequestId() << std::endl; ShutdownSdk(); return -1; } else{ std::cout << "AppendObject success!" << std::endl; } //释放资源 ShutdownSdk();
注意:追加上传过程中,如果目标文件为不可追加文件,则会引发ObjectNotAppendable 异常。