2016-11-07 68 views
1

我需要向AWS ES發出簽名請求,但我陷入了第一個障礙,因爲我似乎無法使用CurlHttpClient。這是我的代碼(verbpathbody別處定義):aws-sdk-cpp:如何使用CurlHttpClient?

Aws::Client::ClientConfiguration clientConfiguration; 
clientConfiguration.scheme = Aws::Http::Scheme::HTTPS; 
clientConfiguration.region = Aws::Region::US_EAST_1; 

auto client = Aws::MakeShared<Aws::Http::CurlHttpClient>(ALLOCATION_TAG, clientConfiguration); 

Aws::Http::URI uri;  
uri.SetScheme(Aws::Http::Scheme::HTTPS);  
uri.SetAuthority(ELASTIC_SEARCH_DOMAIN);  
uri.SetPath(path); 

Aws::Http::Standard::StandardHttpRequest req(uri, verb); 
req.AddContentBody(body); 

auto res = client->MakeRequest(req); 

Aws::Http::HttpResponseCode resCode = res->GetResponseCode();  
if (resCode == Aws::Http::HttpResponseCode::OK) { 
    Aws::IOStream &body = res->GetResponseBody(); 
    rejoiceAndBeMerry(); 
} 
else { 
    gotoPanicStations(); 
} 

在執行時,代碼拋出一個bad_function_call深從混合了大量的shared_ptr此的SDK內並分配這一點。我的猜測是,我只是使用SDK錯誤,但我一直無法找到直接使用CurlHttpClient的任何示例,例如我需要在此處執行的操作。

如何使用CurlHttpClient

回答

1

您不應該直接使用HTTP客戶端,而是使用aws-cpp-sdk-es包提供的包裝。像以前的答案一樣,我會推薦評估庫中附帶的測試用例,以瞭解原作者如何實現API(至少在文檔追趕之前)。

我該如何使用CurlHttpClient

您在正確的軌道上使用託管共享資源和輔助函數。只需要創建一個靜態工廠/客戶端來引用。這是一個通用的例子。

using namespace Aws::Client; 
using namespace Aws::Http; 

static std::shared_ptr<HttpClientFactory> MyClientFactory; // My not be needed 
static std::shared_ptr<HttpClient> MyHttpClient; 

// ... jump ahead to method body ... 

ClientConfiguration clientConfiguration; 
MyHttpClient = CreateHttpClient(clientConfiguration); 
Aws::String uri("https://example.org"); 
std::shared_ptr<HttpRequest> req(
       CreateHttpRequest(uri, 
           verb, // i.e. HttpMethod::HTTP_POST 
           Utils::Stream::DefaultResponseStreamFactoryMethod)); 
req.AddContentBody(body); //<= remember `body' should be `std::shared_ptr<Aws::IOStream>', 
          // and can be created with `Aws::MakeShared<Aws::StringStream>("")'; 
req.SetContentLength(body_size); 
req.SetContentType(body_content_type); 
std::shared_ptr<HttpResponse> res = MyHttpClient->MakeRequest(*req); 
HttpResponseCode resCode = res->GetResponseCode();  
if (resCode == HttpResponseCode::OK) { 
    Aws::StringStream resBody; 
    resBody << res->GetResponseBody().rdbuf(); 
    rejoiceAndBeMerry(); 
} else { 
    gotoPanicStations(); 
} 
1

我在使用CurlHttpClient嘗試從S3下載時遇到了完全相同的錯誤。

我固定它通過在CPP SDK中集成測試之後,而不是我的建模代碼:

aws-sdk-cpp/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp 

搜索測試稱爲TestObjectOperationsWithPresignedUrls