You need to enable JavaScript to run this app.
导航
上传回调(C++ SDK)
最近更新时间:2024.03.21 15:13:15首次发布时间:2024.03.21 15:13:15

上传回调是指客户端在请求时携带回调(Callback)参数,服务端在上传完成后,发送同步的 POST 回调请求到 CallBack 中指定的第三方应用服务器,在服务器确认接受并返回结果后,才将所有结果返回给客户端。
关于上传回调的详细介绍,请参见上传回调

示例代码

普通上传实现上传回调

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
    std::string objectName = "exampledir/exampleobject.txt";
    
    // 上传回调参数,需要经过 base64 编码的 Json 格式字符串,具体见上传回调章节详细介绍
    std::string callback = "your callback param";
    // 上传回调参数变量,需要经过 base64 编码的 Json 格式字符串,具体见上传回调章节详细介绍
    std::string callbackVar = "your callback custom variable";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    
    // 需要上传的对象数据,以 stringstream 的形式上传
    std::string data("Object data to be uploaded");
    auto ss = std::make_shared<std::stringstream>(data);
    PutObjectV2Input input(bucketName, objectName, ss);
    // 传入上传回调相关参数
    input.setCallBack(callback);
    input.setCallBackVar(callbackVar);
    
    auto output = client.putObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "PutObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "PutObject success. the object etag:" << output.result().getETag() << std::endl;

    // 释放网络等资源
    CloseClient();
    return 0;
}

分片上传实现上传回调

#include "TosClientV2.h"
using namespace VolcengineTos;

static int64_t getFileSize(const std::string& file)
{
    std::fstream f(file, std::ios::in | std::ios::binary);
    f.seekg(0, f.end);
    int64_t size = f.tellg();
    f.close();
    return size;
}

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
    std::string objectName = "exampledir/exampleobject.txt";
    // 上传回调参数,需要经过 base64 编码的 Json 格式字符串,具体见上传回调章节详细介绍
    std::string callback = "your callback param";
    // 上传回调参数变量,需要经过 base64 编码的 Json 格式字符串,具体见上传回调章节详细介绍
    std::string callbackVar = "your callback custom variable";
    
     // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    // 初始化分片上传事件
    CreateMultipartUploadInput input(bucketName, objectName);
    // 可以指定 ACL,StorageClass,用户自定义元数据等
    input.setAcl(ACLType::PublicRead);
    input.setStorageClass(StorageClassType::IA);
    input.setMeta({{"self-test", "yes"}});
    auto upload = client.createMultipartUpload(input);
    if (!upload.isSuccess()) {
        // 异常处理
        std::cout << "createMultipartUpload failed." << upload.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "createMultipartUpload success." << std::endl;

    // 准备上传的文件
    std::string fileToUpload = "yourLocalFilename";
    // 5MB
      int64_t partSize = 5 * 1024 * 1024;
    std::vector<UploadedPartV2> partResList;
    auto fileSize = getFileSize(fileToUpload);
    int partCount = static_cast<int>(fileSize / partSize);
    // 计算分片个数
    if (fileSize % partSize != 0) {
        partCount++;
    }

    // 对每一个分片进行上传
    auto uploadId = upload.result().getUploadId();
    for (int i = 1; i <= partCount; i++) {
        auto offset = partSize * (i - 1);
        auto size = (partSize < fileSize - offset) ? partSize : (fileSize - offset);
        std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(fileToUpload, std::ios::in|std::ios::binary);
        content->seekg(offset, std::ios::beg);

        UploadPartV2Input uploadPartInput(bucketName, objectName,uploadId, size,i,content);
        auto uploadPartOutput = client.uploadPart(uploadPartInput);
        if (!uploadPartOutput.isSuccess()) {
            std::cout << "uploadPart failed." << upload.error().String() << std::endl;
            // 释放网络等资源
                CloseClient();
            return -1;
        }

        UploadedPartV2 part(i, uploadPartOutput.result().getETag());
        partResList.push_back(part);
    }

    // 完成分片上传
    CompleteMultipartUploadV2Input completeMultipartUploadInput(bucketName, objectName, uploadId, partResList);
    auto com = client.completeMultipartUpload(completeMultipartUploadInput);
    // 传入上传回调相关参数
    completeMultipartUploadInput.setCallBack(callback);
    completeMultipartUploadInput.setCallBackVar(callbackVar);
    
    if (!com.isSuccess()) {
        // 异常处理
        std::cout << "CompleteMultipartUpload failed." << upload.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "CompleteMultipartUpload success." << com.result().getRequestInfo().getRequestId() << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}