Uploading file to AWS using C++ SDK
Asked Answered
T

1

9

Whenever I run my program I get the error "Unable to connect to endpoint". But I know that I can connect to that bucket because I can successfully download files that are in that bucket using the same configurations and s3Client. It gets stuck on the PutObject() line.

Here is the code.

const String KEY = "test2.txt";
const String BUCKET = "savefiles2017";


const String fileName = "test2.txt";

Client::ClientConfiguration config;
config.region = Region::US_WEST_2;
config.scheme = Http::Scheme::HTTPS;

S3Client s3Client(Auth::AWSCredentials("XXXXXX", "YYYYYY"), config); 

//putting something into s3
PutObjectRequest putObjectRequest;
putObjectRequest.WithBucket(BUCKET).WithKey(KEY);

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in);

putObjectRequest.SetBody(requestStream);

auto putObjectOutcome = s3Client.PutObject(putObjectRequest);

if (putObjectOutcome.IsSuccess())
{
    cout << "Put object succeeded" << endl;
}
else
{
    cout << "Error while putting Object " << putObjectOutcome.GetError().GetExceptionName() <<
        " " << putObjectOutcome.GetError().GetMessage() << endl;
}

These are the includes that I am using as well

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h> 


using namespace Aws;
using namespace Aws::S3;
using namespace Aws::S3::Model;

Any help would be greatly appreciated.

Tanguay answered 22/6, 2017 at 2:29 Comment(3)
What version of the SDK? Can you post trace logs?Mitis
I am using NuGet so for the AWSSDKCPP-Core it is version 1.0.128 and for AWSSDKCPP-S3 it is version 1.0.20060301.128. How do I find the trace logs? Also I am running on Visual Studios.Tanguay
It also seems to work if I upload an empty file. But if I put any contents in the file it doesn't upload.Tanguay
T
4

I figured out how to fix it. I had to change the line

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in);

To

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in | ios_base::binary);

So I just had to add the extra flag of ios_base::binary

Tanguay answered 22/6, 2017 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.