Java中的PresignedURL的SignatureDoesNotMatch

时间:2019-02-26 05:59:21

标签: java amazon-web-services amazon-s3 aws-sdk pre-signed-url

public S3PresignedURLServiceImpl() {
        amazonS3Client = AmazonS3ClientBuilder
                .standard()
                .withCredentials(new DefaultAWSCredentialsProviderChain())
                .withRegion(S3PresignedURLConstants.DEFAULT_REGION)
                .build();
    }

[在本地运行,但是在控制台上部署了lambda时会获得

  

SignatureDoesNotMatch我们计算出的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

try {
    // Set the pre-signed URL to expire after specified time.
    java.util.Date expiration = new java.util.Date();
    long expTimeMillis = expiration.getTime();
    if(data.getExpiryTime() > 0) {
        expTimeMillis += 1000 * 60 * data.getExpiryTime();
    } else {
        expTimeMillis += 100 * 60 * 60 * 6;
    }
    expiration.setTime(expTimeMillis);
    HttpMethod httpMethod = data.isUpload()?HttpMethod.PUT:HttpMethod.GET;

    Logger.logInfo("Generating pre-signed URL.",REPORTER);
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(data.getBucketName(), data.getFilePath())
            .withMethod(httpMethod)
            .withExpiration(expiration);
    if(!data.isUpload())
        generatePresignedUrlRequest.withVersionId(data.getVersionId());
    else generatePresignedUrlRequest.withContentType(data.getContentType());

    url = amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest);
    responseData.setPreSignedUrl(url.toString());
}
catch(Exception e) {
    throw new S3PresignedURLException(e.getMessage(), e);
}

Screenshot

1 个答案:

答案 0 :(得分:0)

请检查库org.apache.httpcomponents:httpclient的版本,如果是4.5.74.5.8,请尝试降级到4.5.6,因为AWS SDK S3存在问题。有关更多详细信息,请查看Amazon S3 Signature Does Not Match - AWS SDK Javaorg.apache.httpcomponents:httpclient:4.5.7 breaks fetching S3 objects