使用C++ SDK将文件上传到AWS

使用C++ SDK将文件上传到AWS

问题描述:

每当我运行我的程序时,都会收到错误“无法连接到端点”。但我知道我可以连接到该存储桶,因为我可以使用相同的配置和s3Client成功下载该存储桶中的文件。它被卡在PutObject()行上。使用C++ SDK将文件上传到AWS

这是代码。

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; 
} 

这些都是包括我使用以及

#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; 

任何帮助将不胜感激。

+0

的SDK版本有什么额外的标志?你能发布跟踪日志吗? –

+0

我使用NuGet,因此对于AWSSDKCPP-Core,它是版本1.0.128,对于AWSSDKCPP-S3,它是版本1.0.20060301.128。我如何找到跟踪日志?我也在Visual Studios上运行。 – karllawson

+0

它也似乎工作,如果我上传一个空文件。但是,如果我将任何内容放入文件中,它都不会上传。 – karllawson

我想出了如何解决它。我不得不改变线路

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

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

所以我就不得不添加的ios_base::binary