aws-sdk-cpp: PutObjectRequest + Aws::FStream give inconsistent output

What platform/OS are you using?

Linux (Ubuntu 16.04)

What compiler are you using? what version?

gcc 5.4

What’s your CMake arguments?

-DBUILD_ONLY="s3"

Issue

The PutObject request gives content length as -1 for every odd index in the following code with the file being constant:

    auto inpData = Aws::MakeShared<Aws::FStream>("PutObjectInputStream",
            path.c_str(), std::ios_base::in | std::ios_base::binary);
    auto objReq = Aws::S3::Model::PutObjectRequest();
    objReq.WithBucket(bucketName).SetBody(inpData);
    for (size_t i = 0; i < loop; ++i)
    {
        String key_suffix = std::to_string(i).c_str();
        auto objRes = client->PutObject(objReq.WithKey(key + key_suffix));
    }

Some details about types and variables used:

    using String = Aws::String;
    String key = "some_key", path = "SomePath";
    size_t loop = 11;
    std::shared_ptr<Aws::S3::S3Client> client;

Expected behavior

Consistent content length and content for every request (provided that the file doesn’t change) without instantiating new instances for every loop. However, this is my personal expectation.

Current Solution

    using String = Aws::String;
    String key = "some_key", path = "SomePath";
    size_t loop = 11;
    for (size_t i = 0; i < loop; ++i)
    {
        auto inpData = Aws::MakeShared<Aws::FStream>("PutObjectInputStream",
                path.c_str(), std::ios_base::in | std::ios_base::binary);
        auto objReq = Aws::S3::Model::PutObjectRequest();
        objReq.WithBucket(bucketName).SetBody(inpData);
        String key_suffix = std::to_string(i).c_str();
        auto objRes = client->PutObject(objReq.WithKey(key + key_suffix));
    }

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 25 (10 by maintainers)

Most upvoted comments

I guess this is because once SDK is done with your iostream – it is in “I point to the end of file” state. So, when SDK tries to use it second time – it run’s into eof() condition (when calculating content-length) and as a side effect resets pointer to the start of file.

Try resetting it manually in every iteration – I bet problem will go away.