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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With