aws-sdk-cpp:如何使用CurlHttpClient?
问题描述:
我需要向AWS ES发出签名请求,但我陷入了第一个障碍,因为我似乎无法使用CurlHttpClient
。这是我的代码(verb
,path
和body
别处定义):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
?
答
您不应该直接使用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();
}
答
我在使用CurlHttpClient
尝试从S3下载时遇到了完全相同的错误。
我固定它通过在CPP SDK中集成测试之后,而不是我的建模代码:
aws-sdk-cpp/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp
搜索测试称为TestObjectOperationsWithPresignedUrls
。