Interface AwsV4HttpSigner

All Superinterfaces:
AwsV4FamilyHttpSigner<AwsCredentialsIdentity>, HttpSigner<AwsCredentialsIdentity>
All Known Implementing Classes:
DefaultAwsV4HttpSigner

public interface AwsV4HttpSigner extends AwsV4FamilyHttpSigner<AwsCredentialsIdentity>
An HttpSigner that will use the AWS V4 signing algorithm to sign a request using an AwsCredentialsIdentity).

The steps performed by this signer are documented here.

Using the AwsV4HttpSigner

Sign an HTTP request and send it to a service.

   AwsV4HttpSigner signer = AwsV4HttpSigner.create();

   // Specify AWS credentials. Credential providers that are used by the SDK by default are
   // available in the module "auth" (e.g. DefaultCredentialsProvider).
   AwsCredentialsIdentity credentials =
       AwsSessionCredentialsIdentity.create("skid", "akid", "stok");

   // Create the HTTP request to be signed
   SdkHttpRequest httpRequest =
       SdkHttpRequest.builder()
                     .uri("https://s3.us-west-2.amazonaws.com/bucket/object")
                     .method(SdkHttpMethod.PUT)
                     .putHeader("Content-Type", "text/plain")
                     .build();

   // Create the request payload to be signed
   ContentStreamProvider requestPayload =
       ContentStreamProvider.fromUtf8String("Hello, World!");

   // Sign the request. Some services require custom signing configuration properties (e.g. S3).
   // See AwsV4HttpSigner and AwsV4FamilyHttpSigner for the available signing options.
   //    Note: The S3Client class below requires a dependency on the 's3' module. Alternatively, the
   //    signing name can be hard-coded because it is guaranteed to not change.
   SignedRequest signedRequest =
       signer.sign(r -> r.identity(credentials)
                         .request(httpRequest)
                         .payload(requestPayload)
                         .putProperty(AwsV4HttpSigner.SERVICE_SIGNING_NAME, S3Client.SERVICE_NAME)
                         .putProperty(AwsV4HttpSigner.REGION_NAME, "us-west-2")
                         .putProperty(AwsV4HttpSigner.DOUBLE_URL_ENCODE, false) // Required for S3 only
                         .putProperty(AwsV4HttpSigner.NORMALIZE_PATH, false)); // Required for S3 only

   // Create and HTTP client and send the request. ApacheHttpClient requires the 'apache-client' module.
   try (SdkHttpClient httpClient = ApacheHttpClient.create()) {
       HttpExecuteRequest httpExecuteRequest =
           HttpExecuteRequest.builder()
                             .request(signedRequest.request())
                             .contentStreamProvider(signedRequest.payload().orElse(null))
                             .build();

       HttpExecuteResponse httpResponse =
           httpClient.prepareRequest(httpExecuteRequest).call();

       System.out.println("HTTP Status Code: " + httpResponse.httpResponse().statusCode());
   } catch (IOException e) {
       System.err.println("HTTP Request Failed.");
       e.printStackTrace();
   }
  • Field Details

    • REGION_NAME

      static final SignerProperty<String> REGION_NAME
      The AWS region name to be used for computing the signature. This property is required.
  • Method Details