我剛開始用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的一些資料或方法可以分享,萬分感謝!
uj5u.com熱心網友回復:
兄弟,我也遇到了一樣的問題,也是這個例程,你這個問題是怎么解決掉的我的顯示 can not connect to endpoint
uj5u.com熱心網友回復:
key和bucket是要自己去申請的吧,還是說例程上面那個就可以用uj5u.com熱心網友回復:
能說一下怎么在linux下編譯安裝aws-sdk-cpp原始碼包?uj5u.com熱心網友回復:
能說一下怎么在linux下編譯安裝aws-sdk-cpp原始碼包?uj5u.com熱心網友回復:
AWS是使用credentials來識別身份的,所以你要先注冊一個AWS的賬號,第一年很多服務免費使用,然后用你賬號的public security key 和 private security key在你的系統上配置credentials。你可以參考:https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/credentials.html另外,其它使用c++開發aws的例子也可以參考該頁面的其它鏈接,講的更系統。
uj5u.com熱心網友回復:
我碰到過類似問題,你需要把SDK重新編譯一遍加上選項-DCUSTOM_MEMORY_MANAGEMENT=OFF。uj5u.com熱心網友回復:
key和bucket 要先創建好,才能用, 當然要最先去注冊帳號。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/19047.html
標籤:AWS
