由于授权无法访问 S3 预签名 URL [英] Can't access S3 Pre-Signed URL due to authorization

查看:33
本文介绍了由于授权无法访问 S3 预签名 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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

谢谢

推荐答案

使用一些帮助我找到了答案,这是 2 个缺失部分的组合(其中一个在评论中被提及):

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. 需要设置:

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆