aliyun / aliyun-oss-cpp-sdk

Aliyun OSS SDK for C++
Other
188 stars 88 forks source link

关于Meta的疑问:在C++下如何在上传文件时设置自定义meta? #136

Closed jinyunshaobing closed 2 years ago

jinyunshaobing commented 2 years ago

有无样例代码呢?我现在创建ObjectMetaData后直接传给PutObjectRequest会导致SignatureDoesNotMatch错误

FileProviderResult OSSFileProvider::putFile(std::string path, std::string content, std::map<std::string, std::string> metaData)
{
    auto logger = SidecarLoggers::getLogger("oss_file_provider");
    logger->debug("uploading file : {}", path);

    std::shared_ptr<std::iostream> stream = std::make_shared<std::stringstream>();
    *stream << content;
    AlibabaCloud::OSS::ObjectMetaData meta;
    auto iter = metaData.begin();
    while(iter!=metaData.end()){
        meta.addUserHeader(iter->first,iter->second);
        ++iter;
    }

    AlibabaCloud::OSS::PutObjectRequest request(this->bucket, path, stream, meta);
    // AlibabaCloud::OSS::PutObjectRequest request(this->bucket, path, stream);

    auto outcome = this->client->PutObject(request);
    FileProviderResult result;
    result.requestId = outcome.result().RequestId();
    result.eTag = outcome.result().ETag();
    if (outcome.isSuccess())
    {
        result.success = true;
        logger->debug("upload file succeed, file path : {}", path);
    }
    else
    {
        result.success = false;
        result.errorCode = outcome.error().Code();
        result.errorMessage = outcome.error().Message();
        logger->warn("upload file failed, file path : {}, error code : {}, error message : {}", path, result.errorCode, result.errorMessage);
    }
    return result;
}
jinyunshaobing commented 2 years ago

我也尝试过另一种写法,就是在创建request时不传入meta,在创建后再添加,但是还是会有同样的问题:

    AlibabaCloud::OSS::PutObjectRequest request(this->bucket, path, stream);
    request.MetaData().addUserHeader();
jinyunshaobing commented 2 years ago

我解决了……对于metaData而言,一定不要在key中包含_,只能用-,这种限制也太难注意到了 I solved it, use - instead of _ will solve the SignatureDoesNotMatch caused by metaData.