1

I want to perform Multithreading to upload objects and get a callable message when using AWS S3 SDK for C++.

Eg.: When I am uploading 10 images to my s3 bucket, a different thread is incrementing a counter. When the uploading is finished, I should get a callable object or pointer which will stop my counter increment.

I want a Thread Safe Library so that I can upload/download or process files parallely

PutObjectCallableRequest class is available but the parameter types and definition is not defined. How How do I perform this task. I don't want to use any APIs, use of s3 SDK library is necessary.

I have attached a link to example code of uploading an object using PutObjectRequest in aws s3 SDK for C++.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/cpp/example_code/s3/put_object.cpp

This is supposed to be one thread. In case I also want to list objects of my bucket in parallel , what library should I use?

Edit:

This is what I am trying to implement. How do I make changes in the following code to get asynchronous operations implemented and get a callback.

#include <aws/core/Aws.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/http/Scheme.h>
#include <aws/core/utils/threading/Executor.h>
#include <aws/s3/S3Client.h>
#include <aws/transfer/TransferManager.h>

using namespace std;
using namespace Aws::S3;
using namespace Aws::S3::Model;
using namespace Aws::Client;
using namespace Aws::Http;


static const char* const ALLOCATION_TAG = "S3_MULTIPART_TEST";

int main(int /*argc*/, char** /*argv*/)
{
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    Aws::String bucket = "your-bucket-name";

    // Set up S3 client
    ClientConfiguration config;
    config.region = "us-east-1";
    config.connectTimeoutMs = 3000;
    config.requestTimeoutMs = 60000;
    auto s3Client = Aws::MakeShared<Aws::S3Client>(ALLOCATION_TAG, config, false);

   // Aws::S3::S3Client s3Client(config);
    auto sdk_client_executor = Aws::MakeShared<Aws::Utils::Threading::DefaultExecutor>(ALLOCATION_TAG);
    Aws::Transfer::TransferManagerConfiguration transferConfig(sdk_client_executor.get());
    transferConfig.s3Client = s3Client;


    transferConfig.uploadProgressCallback =
    [](const Aws::Transfer::TransferManager*, const std::shared_ptr<const Aws::Transfer::TransferHandle>&transferHandle)
{ std::cout << "Upload Progress: " << transferHandle->GetBytesTransferred() << " of " << transferHandle->GetBytesTotalSize() << " bytes\n"; };

    static std::shared_ptr<Aws::Transfer::TransferManager> transferManager = Aws::Transfer::TransferManager::Create(transferConfig);

    Aws::String fileToUpload = "MyFile.data";
std::shared_ptr<Aws::Transfer::TransferHandle> transferHandle = transferManager->UploadFile(fileToUpload.c_str(), bucket.c_str(), fileToUpload.c_str(),
    "multipart/form-data", Aws::Map<Aws::String, Aws::String>());

    size_t retries = 0;
    transferHandle->WaitUntilFinished();
    while (transferHandle->GetStatus() == Aws::Transfer::TransferStatus::FAILED && retries++ < 5)
    {
        transferManager->RetryUpload(fileToUpload.c_str(), transferHandle);
        transferHandle->WaitUntilFinished();
    }
    transferHandle->SetIsMultipart(true);

    Aws::Transfer::PartStateMap completedParts = transferHandle->GetCompletedParts();
    string eTagList;

    for (const auto &p : completedParts)
    {
        std::cout << "completedparts[" << p.first << "]" << p.second->GetETag() << '\n';
    }

    Aws::Transfer::TransferStatus status = transferHandle->GetStatus();
    std::string res("Result:");
    res.append(std::to_string((int)status));

    std::cout << res.c_str() << std::endl;

    Aws::ShutdownAPI(options);
    return 0;
}

This code is not working.

error:no matching function for call to ‘Aws::S3::S3Client::S3Client(Aws::Client::ClientConfiguration&, bool)’

error in line: auto s3Client = Aws::MakeShared<Aws::S3Client>(ALLOCATION_TAG, config, false);
Arrow
  • 11
  • 3

1 Answers1

0

Use Transfer Manager.

And look at the tests for example usage.

Marco M.
  • 2,956
  • 2
  • 29
  • 22