gpt4 book ai didi

java - 使用 AWS4Signer 签署 Get 请求 : The request signature we calculated does not match the signature you provided

转载 作者:行者123 更新时间:2023-12-02 10:52:38 25 4
gpt4 key购买 nike

上下文

我正在尝试生成 GET 请求来获取存储在我的 AWS S3 存储桶中的文件。生成的链接将在一小时后过期。

其他详细信息

  • API:使用适用于 Java 的 Amazon V2 SDK - 开发人员预览版 11
  • 此错误通常也是由于访问键中包含“+”而引起的,但我的键中都没有它们

相关代码

@Override
public CompletableFuture<String> getSignedUrl(String key) {
Aws4PresignerParams params = Aws4PresignerParams.builder()
.awsCredentials(this.credentials)
.signingName("s3")
.signingRegion(Region.AP_SOUTH_1)
.expirationTime(Instant.ofEpochSecond(60*60))
.build();
SdkHttpFullRequest request = SdkHttpFullRequest.builder()
.host("s3.ap-south-1.amazonaws.com")
.encodedPath("<bucket name>/" + key)
.method(SdkHttpMethod.GET)
.protocol("https")
.build();
SdkHttpFullRequest result = Aws4Signer.create().presign(request,params);
return CompletableFuture.completedFuture(result.getUri().toString());
}

错误输出:

访问 key 已替换为<访问 key >

<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>
<AWSAccessKeyId><Access Key></AWSAccessKeyId>
<StringToSign>
AWS4-HMAC-SHA256 20180828T031727Z 20180828/ap-south-1/s3/aws4_request ec1203b0c718bc306b351e17e6014965860a9af6743a559f986af8fbbee0845d
</StringToSign>
<SignatureProvided>
6e6abcdd52db1746723446f41566f1102072b42a8230a08fe7f1d555e98c931a
</SignatureProvided>
<StringToSignBytes>
41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 31 38 30 38 32 38 54 30 33 31 37 32 37 5a 0a 32 30 31 38 30 38 32 38 2f 61 70 2d 73 6f 75 74 68 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 65 63 31 32 30 33 62 30 63 37 31 38 62 63 33 30 36 62 33 35 31 65 31 37 65 36 30 31 34 39 36 35 38 36 30 61 39 61 66 36 37 34 33 61 35 35 39 66 39 38 36 61 66 38 66 62 62 65 65 30 38 34 35 64
</StringToSignBytes>
<CanonicalRequest>
GET /<bucket name>/imageTestLogo.svg X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<access key>%2F20180828%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20180828T031727Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host host:s3.ap-south-1.amazonaws.com host UNSIGNED-PAYLOAD
</CanonicalRequest>
<CanonicalRequestBytes>
47 45 54 0a 2f 73 74 67 70 6f 63 6b 65 74 70 69 6c 6c 73 69 6d 61 67 65 73 2f 69 6d 61 67 65 54 65 73 74 4c 6f 67 6f 2e 73 76 67 0a 58 2d 41 6d 7a 2d 41 6c 67 6f 72 69 74 68 6d 3d 41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 26 58 2d 41 6d 7a 2d 43 72 65 64 65 6e 74 69 61 6c 3d 41 4b 49 41 4a 49 41 36 4f 58 4a 36 5a 50 58 34 45 5a 55 51 25 32 46 32 30 31 38 30 38 32 38 25 32 46 61 70 2d 73 6f 75 74 68 2d 31 25 32 46 73 33 25 32 46 61 77 73 34 5f 72 65 71 75 65 73 74 26 58 2d 41 6d 7a 2d 44 61 74 65 3d 32 30 31 38 30 38 32 38 54 30 33 31 37 32 37 5a 26 58 2d 41 6d 7a 2d 45 78 70 69 72 65 73 3d 33 36 30 30 26 58 2d 41 6d 7a 2d 53 69 67 6e 65 64 48 65 61 64 65 72 73 3d 68 6f 73 74 0a 68 6f 73 74 3a 73 33 2e 61 70 2d 73 6f 75 74 68 2d 31 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 0a 68 6f 73 74 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44
</CanonicalRequestBytes>
<RequestId>5FC3A1A9AB1A9231</RequestId>
<HostId>
/w3XcTplWGMQ7VwMQ6C7rIFVs0/kCEX2SD5/0j1MJNBLmByapAns3F3Bz0H0lyzypyrP18tNBlM=
</HostId>
</Error>

最佳答案

新 API 对 Amazon S3 服务使用不同的签名者类:AwsS3V4Signer

 @Override
public CompletableFuture<String> getSignedUrl(String key) {
System.out.println(this.credentials);
Aws4PresignerParams params = Aws4PresignerParams.builder()
.expirationTime(Instant.ofEpochSecond(60*60))
.awsCredentials(this.credentials)
.signingName("s3")
.signingRegion(Region.AP_SOUTH_1)
.build();
SdkHttpFullRequest request = SdkHttpFullRequest.builder()
.encodedPath(("/stgpocketpillsimages/" + key))
.host("s3.ap-south-1.amazonaws.com")
.method(SdkHttpMethod.GET)
.protocol("https")
.build();
SdkHttpFullRequest result = AwsS3V4Signer.create().presign(request,params);
System.out.println(Json.toJson(result));
return CompletableFuture.completedFuture(result.getUri().toString());
}

关于java - 使用 AWS4Signer 签署 Get 请求 : The request signature we calculated does not match the signature you provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com