How to generate Signature in AWS from Java

前端 未结 6 609
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 10:19

When I invoke API endpoints from REST client, I got error by concerning with Signature.

Request:

Host: https://xxx.execute-ap

6条回答
  •  無奈伤痛
    2020-12-24 10:54

    From the code example above it looks like you are not creating a canonical request and including it in the string that gets signed as per http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

    Instead of implementing this yourself have you looked at using a third-party library.

    aws-v4-signer-java is a lightweight, zero-dependency library that makes it easy to generate AWS V4 signatures.

    String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
    HttpRequest request = new HttpRequest("GET", new URI("https://examplebucket.s3.amazonaws.com?max-keys=2&prefix=J"));
    String signature = Signer.builder()
            .awsCredentials(new AwsCredentials(ACCESS_KEY, SECRET_KEY))
            .header("Host", "examplebucket.s3.amazonaws.com")
            .header("x-amz-date", "20130524T000000Z")
            .header("x-amz-content-sha256", contentSha256)
            .buildS3(request, contentSha256)
            .getSignature();
    

    Disclaimer: I'm the libraries author.

提交回复
热议问题