Home > Blockchain >  Unable to compile example code from AWS SDK for C - Developer Guide
Unable to compile example code from AWS SDK for C - Developer Guide

Time:06-08

I am trying to upload a file using an encrypted client and i`m having a hard time setting the body of the PutObjectRequest object. I am using the sample code.

PutObjectRequest putObjectRequest;
    putObjectRequest.WithBucket("BUCKET_NAME")
        .WithKey(AES_MASTER_KEY);
    std::shared_ptr<Aws::IOStream> input_data =
        Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
            FILE_NAME,
            std::ios_base::in | std::ios_base::binary);
    putObjectRequest.SetBody(input_data);

but I am getting the following error:

no suitable user-defined conversion from "std::shared_ptrAws::FStream" to "std::shared_ptrAws::IOStream" exists

The original code can be found at https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/aws-sdk-cpp-dg.pdf , page 180

This is the full function:

#include <aws/core/Aws.h>
#include <aws/core/utils/logging/LogLevel.h>
#include <aws/s3/S3Client.h>
#include <aws/s3-encryption/materials/SimpleEncryptionMaterials.h>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/s3-encryption/S3EncryptionClient.h>
#include <aws/s3-encryption/CryptoConfiguration.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>

#include <iostream>
using namespace Aws;
using namespace Aws::S3::Model;


bool PutObject(const Aws::String& bucketName,
    const Aws::String& objectName)
{
    // Verify that the file exists.
    struct stat buffer;
    if (stat(objectName.c_str(), &buffer) == -1)
    {
        std::cout << "Error: PutObject: File '" <<
            objectName << "' does not exist." << std::endl;
        return false;
    }
    Aws::Client::ClientConfiguration config;
    Aws::S3::S3Client s3_client(config);

    Aws::S3::Model::PutObjectRequest request;
    request.SetBucket(bucketName);
    //We are using the name of the file as the key for the object in the bucket.
    //However, this is just a string and can set according to your retrieval needs.
    request.SetKey(objectName);
    std::shared_ptr<Aws::IOStream> input_data =
        Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
            objectName.c_str(),
            std::ios_base::in | std::ios_base::binary);
    request.SetBody(input_data);
    Aws::S3::Model::PutObjectOutcome outcome =
        s3_client.PutObject(request);
    if (outcome.IsSuccess()) {
        std::cout << "Added object '" << objectName << "' to bucket '"
            << bucketName << "'.";
        return true;
    }
    else
    {
        std::cout << "Error: PutObject: " <<
            outcome.GetError().GetMessage() << std::endl;

        return false;
    }
}

That is the only error I get Image showing build error

CodePudding user response:

As the code is missing #include <fstream> Aws::FStream (which is a typedef for std::fstream) is an incomplete type and the compiler doesn't know that std::fstream is derived from std::iostream so doesn't know how to convert from std::shared_ptr<Aws::FStream> to std::shared_ptr<Aws::IOStream>.

Visual studio isn't very helpful here, clang and GCC at least tell you that std::fstream is an incomplete type: https://godbolt.org/z/Paeqj5saW

  • Related