aws-sdk-cpp:如何使用CurlHttpClient?

时间:2016-11-07 04:06:17

标签: aws-sdk

我需要向AWS ES发出已签名的请求,但我遇到了第一个障碍,因为我似乎无法使用CurlHttpClient。以下是我的代码(verbpath和其他地方定义的body):

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

执行时,代码会从sdk中抛出一个bad_function_call深的混合了大量的shared_ptr,然后分配它。我的猜测是我错误地使用了SDK,但是我无法找到任何直接使用CurlHttpClient的示例,例如我需要在这里做的。

如何使用CurlHttpClient

2 个答案:

答案 0 :(得分:3)

您不应该直接使用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 :(得分:1)

尝试使用CurlHttpClient从S3下载时遇到完全相同的错误。

我通过在cpp sdk中找到集成测试后对代码进行建模来修复它:

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

搜索名为TestObjectOperationsWithPresignedUrls的测试。