1

I need to make signed requests to AWS ES, but am stuck at the first hurdle in that I cannot seem to be able to use CurlHttpClient. Here is my code (verb, path, and body defined elsewhere):

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

When executed, the code throws a bad_function_call deep from within the sdk mixed up with a lot of shared_ptr this and allocate that. My guess is that I am just using the SDK wrong, but I've been unable to find any examples that use the CurlHttpClient directly such as I need to do here.

How can I use CurlHttpClient?

2 Answers 2

4

You shouldn't be using the HTTP client directly, but the supplied wrappers with the aws-cpp-sdk-es package. Like previous answer(s), I would recommend evaluating the test cases shipped with the library to see how the original authors intended to implement the API (at least until the documents catch-up).

How can I use CurlHttpClient?

Your on the right track with managed shared resources and helper functions. Just need to create a static factory/client to reference. Here's a generic example.

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your code works perfectly for a string but how would you do this for a file? I am not able to upload a file with a stream. A file is created on S3 but sadly with 0 Bytes.
1

I encountered exactly the same error when trying to download from S3 using CurlHttpClient.

I fixed it by instead modelling my code after the integration test found in the cpp sdk:

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

Search for the test called TestObjectOperationsWithPresignedUrls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.