Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file to AWS using C++ SDK

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.

like image 804
karllawson Avatar asked Jun 22 '17 02:06

karllawson


People also ask

How do I upload files to AWS?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

How do I upload a PDF to AWS?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.

How do I upload local files to AWS S3?

You have two options for uploading files: AWS Management Console: Use drag-and-drop to upload files and folders to a bucket. AWS CLI: With the version of the tool installed on your local machine, use the command line to upload files and folders to the bucket.


1 Answers

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

like image 131
karllawson Avatar answered Nov 08 '22 21:11

karllawson