请教用AWS的C++版SDK开发所遇到的问题,在线等!
我刚开始用AWS的C++版SDK开发,目的是在Linux环境下,开发一个调用AWS-SDK上传文件到云端的功能模块。源码包是aws-sdk-cpp-master.zip,我已将源码包编译安装成功。但由于C++的调用例程几乎没有,我看那些接口代码也看得一头雾水。我在官网上(https://aws.amazon.com/cn/blogs/developer/using-cmake-exports-with-the-aws-sdk-for-c/)找到这么一段例程,我将这段代码保存为文件TestUseAWS.cpp:
#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::S3;
using namespace Aws::S3::Model;
static const char* KEY = "s3_cpp_sample_key";
static const char* BUCKET = "s3-cpp-sample-bucket";
int main()
{
S3Client client;
//first put an object into s3
PutObjectRequest putObjectRequest;
putObjectRequest.WithKey(KEY)
.WithBucket(BUCKET);
//this can be any arbitrary stream (e.g. fstream, stringstream etc...)
auto requestStream = Aws::MakeShared<Aws::StringStream>("s3-sample");
*requestStream << "Hello World!";
//set the stream that will be put to s3
putObjectRequest.SetBody(requestStream);
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;
}
return 0;
}
以上代码在我机器上都编译连接通过了,但在运行时报错,我输入的命令为:
(1)g++ -c -w -std=c++11 -I aws-sdk-cpp-master/aws-cpp-sdk-core/include -I aws-sdk-cpp-master/aws-cpp-sdk-s3/include TestUseAWS.cpp
(2)g++ -o TestUseAWS -L /usr/local/lib/linux/ia32 TestUseAWS.o -laws-cpp-sdk-core -laws-cpp-sdk-s3
(3)Export LD_LIBRARY_PATH=/usr/local/lib/linux/ia32:$LD_LIBRARY_PATH
(4)./TestUseAWS
报错为:
*** Error in './TestUseAWS':free():invalid pointer:0x00e31064 *** Aborted
有以下疑问:
(1)设断点发现,在第一句S3Client client;就报错了,不知道是怎么回事;
(2)从putObjectRequest.WithKey(KEY).WithBucket(BUCKET);这一句来看,好像是只需要提供Key和BUCKET就可以将信息PUT上去了,但登录AWS难道不需要账户名吗?
请问论坛里有哪位大神用过C++的AWS-SDK,能帮忙解答下吗?另外,有无关于用C++SDK的一些资料或方法可以分享,万分感谢!