由于授权,无法访问S3预签名URL [英] Can't access S3 Pre-Signed URL due to authorization
问题描述
使用Java8和aws-java-sdk 1.10.43我正在尝试获取一个S3文件的预签名URL。我确实找回了一个链接,但浏览它会导致此错误:
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:
不支持您提供的授权机制。请使用
AWS4-HMAC-SHA256
authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256
为了强调,我希望生成一个可以通过电子邮件发送的URL在浏览器中打开,而不是使用Java代码从该URL读取。
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.
我正在使用波纹管代码,我相信我发现我需要以某种方式设置setSSEAlgorithm使用v4,但是我没能使它工作。我错过了什么?我应该配置什么(注意:我故意避免配置文件,我希望代码从环境变量设置所有属性)
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);
桶位于eu-central-1
The bucket is at eu-central-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):
-
需要设置此项:
Need to set this:
System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
必须设置endPoint(上传或下载不需要):
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));
这篇关于由于授权,无法访问S3预签名URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!