aws-sdk-cpp:如何使用 CurlHttpClient?
aws-sdk-cpp: how to use CurlHttpClient?
我需要向 AWS ES 发出签名请求,但遇到了第一个障碍,因为我似乎无法使用 CurlHttpClient
。这是我的代码(verb
、path
和 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 this 和 allocate that 混合在一起。我的猜测是我只是错误地使用了 SDK,但我一直无法找到任何直接使用 CurlHttpClient
的示例,例如我需要在此处执行的操作。
如何使用CurlHttpClient
?
我在尝试使用 CurlHttpClient
从 S3 下载时遇到了完全相同的错误。
我通过在 cpp sdk 中找到的集成测试后对我的代码进行建模来修复它:
aws-sdk-cpp/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp
搜索名为 TestObjectOperationsWithPresignedUrls
的测试。
您不应直接使用 HTTP 客户端,而应使用 aws-cpp-sdk-es
包提供的包装器。与之前的答案一样,我建议评估库随附的测试用例,以了解原作者打算如何实施 API(至少在文档赶上之前)。
How can I use CurlHttpClient
?
您的托管共享资源和帮助函数走在了正确的轨道上。只需要创建一个 static factory/client 来引用。这是一个通用示例。
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();
}
我需要向 AWS ES 发出签名请求,但遇到了第一个障碍,因为我似乎无法使用 CurlHttpClient
。这是我的代码(verb
、path
和 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 this 和 allocate that 混合在一起。我的猜测是我只是错误地使用了 SDK,但我一直无法找到任何直接使用 CurlHttpClient
的示例,例如我需要在此处执行的操作。
如何使用CurlHttpClient
?
我在尝试使用 CurlHttpClient
从 S3 下载时遇到了完全相同的错误。
我通过在 cpp sdk 中找到的集成测试后对我的代码进行建模来修复它:
aws-sdk-cpp/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp
搜索名为 TestObjectOperationsWithPresignedUrls
的测试。
您不应直接使用 HTTP 客户端,而应使用 aws-cpp-sdk-es
包提供的包装器。与之前的答案一样,我建议评估库随附的测试用例,以了解原作者打算如何实施 API(至少在文档赶上之前)。
How can I use
CurlHttpClient
?
您的托管共享资源和帮助函数走在了正确的轨道上。只需要创建一个 static factory/client 来引用。这是一个通用示例。
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();
}