代码之家  ›  专栏  ›  技术社区  ›  User12547645

Minio:为什么我的PUT请求失败了,即使GET成功了?

  •  0
  • User12547645  · 技术社区  · 6 年前

    我在用 Minio 模拟S3并在本地测试我的代码。我的代码是用 AWS SDK for C++ .

    Unable to connect to endpoint . 不过,我可以使用curl将对象放到Minio中。

    S3Client (我添加了一些喜欢解释我为什么做这些事情的内容):

    auto credentialsProvider = Aws::MakeShared<Aws::Auth::EnvironmentAWSCredentialsProvider>("someTag");
    Aws::Client::ClientConfiguration config;
    config.endpointOverride = Aws::String("172.17.0.2:9000");
    config.scheme = Aws::Http::Scheme::HTTP;
    // diable ssl https://github.com/aws/aws-sdk-cpp/issues/284
    config.verifySSL = false;
    // set region to default https://github.com/awslabs/amazon-kinesis-producer/issues/66
    config.region = "us-east-1"; 
    // disable virtual adress and signing https://stackoverflow.com/questions/47105289/how-to-override-endpoint-in-aws-sdk-cpp-to-connect-to-minio-server-at-localhost
    Aws::S3::S3Client client(credentialsProvider, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); 
    

    这就是我的GET和PUT请求的样子。得到工作,得到不:

    // declare request
    Aws::S3::Model::GetObjectRequest get_obj_req;
    get_obj_req.WithBucket("someBucket").WithKey("someKey");
    
    // Get works fine
    auto get_object_outcome = client.GetObject(get_obj_req);
    if (!get_object_outcome.IsSuccess()){
        // fail does not happen
    }
    
    // write file from Minio to local file (seems to work fine)
    int size = 1024;
    char buffer[size];
    auto &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody().read(buffer, size);
    std::ofstream out("someFile");
    out << std::string(buffer);
    out.close();
    
    // try to PUT the stored file again
    Aws::S3::Model::PutObjectRequest put_obj_req;
    const std::shared_ptr<Aws::IOStream> input_data =
        Aws::MakeShared<Aws::FStream>("tag", "someFile", std::ios_base::in | std::ios_base::binary);
    put_obj_req.SetBody(input_data);
    put_obj_req.WithBucket("someBucket").WithKey("someKey");
    put_obj_req.SetContentLength(size);
    put_obj_req.SetContentType("application/octet-stream");
    
    // PUT request
    auto resp = client.PutObject(put_obj_req);
    if (!resp.IsSuccess()){
        // fails here
    }
    

    如前所述,我可以使用curl将对象放到Minio中。你可以看看 in this Gist

    旁注:我正在使用 Minio inside a Docker container .

    编辑: 我相信这可能是我想放的数据有问题。如果数据中有。 Content-Type application/octet-stream 我遇到了一个错误,但在使用txt文件时没有遇到这个错误。我当前的代码是这样的,如果我想流除cher之外的任何东西,我假设流会中断。你能确认吗?

    Aws::String content_tye = get_object_outcome.GetResult().GetContentType();
    Aws::IOStream &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody();
    retrieved_file.seekg(0, retrieved_file.end);
    int retrieved_file_size = retrieved_file.tellg();
    retrieved_file.seekg(0, retrieved_file.beg);
    
    char *buffer = new char[retrieved_file_size];
    retrieved_file.read(buffer, retrieved_file_size);
    
    AWS_LOGSTREAM_INFO(TAG, "Retrieved file of size: " + Aws::Utils::StringUtils::to_string(retrieved_file_size));
    
    Aws::StringStream stream(Aws::String(buffer));
    
    const std::shared_ptr<Aws::IOStream> input_data =
        Aws::MakeShared<Aws::StringStream>(TAG, Aws::String(buffer));
    
    0 回复  |  直到 6 年前