Can't access S3 Pre-Signed URL due to authorization [duplicate]

久未见 提交于 2019-12-17 21:29:54

问题


Using Java8 and aws-java-sdk 1.10.43 I'm trying to get a Pre-Signed URL to an S3 file. I do get back a link, but browsing to it lead to this error:

authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256

To emphasize, I wish to generate a URL that can be sent via email and opened in a browser, not to use Java code to read from that URL.

I'm using the bellow code, and I believe to find out that I need to somehow set setSSEAlgorithm to use "v4", however I've failed to make it work. What am I missing? What should I configure (note: I'm avoiding configuration file on purpose, I wish the code to set all attributes from environment variables)

Date expiration = <some date>;
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, targetPath);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expiration);

AmazonS3 s3client = new AmazonS3Client(s3Credentials);
URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);

The bucket is at eu-central-1

Thank you


回答1:


Using some help I've found the answer, which was a combination of 2 missing pieces (one of which was referred to in the comments):

  1. Need to set this:

    System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
    
  2. Must set the "endPoint" (which was not required for upload or download):

    s3Client.setEndpoint(endpoint);
    

Optionally it might be useful to also add this:

s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));


来源:https://stackoverflow.com/questions/34483795/cant-access-s3-pre-signed-url-due-to-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!