aws-sdk-cpp: s3 file upload Error while putting Object Unable to connect to endpoint
Hi I am getting this error while uploading a file. Error: s3 file upload Error while putting Object Unable to connect to endpoint My jpeg file size was 294 KB.
I tried the solution in this link but no improvement, http://stackoverflow.com/questions/38647444/aws-c-s3-sdk-putobjectrequest-unable-to-connect-to-endpoint.
Please help me to fix this one. My code below.
static const char* KEY = “aws_cpp_sdk_upload_test”; // “s3_cpp_sample_key”; static const char* BUCKET = “emojiface-photobooth”; // “s3-cpp-sample-bucket”;
int main() { Aws::Client::ClientConfiguration clientConfig; clientConfig.scheme = Aws::Http::Scheme::HTTPS; clientConfig.region = Aws::Region::US_EAST_1; clientConfig.connectTimeoutMs = 30000;
Aws::SDKOptions options;
Aws::InitAPI(options);
S3Client client(clientConfig);
//first put an object into s3
PutObjectRequest putObjectRequest;
putObjectRequest.WithKey(KEY)
.WithBucket(BUCKET);
string filePATH = "planets.jpg";
std::shared_ptr<Aws::FStream> fileToUpload = Aws::MakeShared<Aws::FStream>("OCRFile", filePATH, ios::in | ios::binary | ios::trunc);
std::streampos fsize = 0;
std::ifstream file(filePATH, std::ios::binary);
fsize = file.tellg();
file.seekg(0, std::ios::end);
fsize = file.tellg() - fsize;
file.close();
putObjectRequest.SetBody(fileToUpload);
putObjectRequest.GetBody()->seekg(0, std::ios::end);
putObjectRequest.SetContentType("image/jpeg");
putObjectRequest.SetContentLength(static_cast<long>(fsize));
std::cout << "Entering into PutObject\n" << std::endl;
auto putObjectOutcome = client.PutObject(putObjectRequest);
if (putObjectOutcome.IsSuccess())
{
std::cout << "Put object succeeded" << std::endl;
}
else
{
std::cout << "Error while putting Object " << putObjectOutcome.GetError().GetExceptionName() <<
" " << putObjectOutcome.GetError().GetMessage() << std::endl;
}
//now get the object back out of s3. The response stream can be overridden here if you want it to go directly to
// a file. In this case the default string buf is exactly what we want.
GetObjectRequest getObjectRequest;
getObjectRequest.WithBucket(BUCKET)
.WithKey(KEY);
auto getObjectOutcome = client.GetObject(getObjectRequest);
if (getObjectOutcome.IsSuccess())
{
std::cout << "Successfully retrieved object from s3 with value: " << std::endl;
//std::cout << getObjectOutcome.GetResult().GetBody().rdbuf() << std::endl << std::endl;;
}
else
{
std::cout << "Error while getting object " << getObjectOutcome.GetError().GetExceptionName() <<
" " << getObjectOutcome.GetError().GetMessage() << std::endl;
}
Aws::ShutdownAPI(options);
return 0;
}
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 16 (7 by maintainers)
ok added binary word & works now.
std::shared_ptrAws::FStream fileToUpload = Aws::MakeSharedAws::FStream(“OCRFile”, filePATH, ios::in | std::ios::binary);