简体   繁体   English

S3 - 上传 - 如何生成预签名的 url,让每个人都能读取 object?

[英]S3 - Upload - how to generate a pre-signed url that gives EVERYONE read access to the object?

I'm trying to provide a pre-signed url that, once the image is uploaded, grants the group Everyone read access to the uplodaded image.我正在尝试提供一个预签名的 url,一旦图像上传,就授予Everyone组对上传图像的读取权限。 So far, I'm generating the pre-signed url with the following steps:到目前为止,我正在通过以下步骤生成预签名的 url:

    val req = GeneratePresignedUrlRequest(params.s3Bucket,"$uuid.jpg",HttpMethod.PUT)
    req.expiration = expiration
    req.addRequestParameter("x-amz-acl","public-read")
    req.addRequestParameter("ContentType","image/jpeg")
    val url: URL = s3Client.generatePresignedUrl(req)

But the image, once I check in S3, does not have the expected read access.但是图像,一旦我签入 S3,就没有预期的读取权限。

The HTTP client that performs the upload needs to include the x-amz-acl: public-read header.执行上传的HTTP客户端需要包含x-amz-acl: public-read header。

In your example, you're generating a request that includes that header. But, then you're generating a presigned URL from that request.在您的示例中,您正在生成一个包含该header的请求。但是,您将从该请求生成一个预签名的 URL。

URLs don't contain HTTP headers, so whatever HTTP client you're using to perform the actual upload is not sending setting the header when it sends the request to the generated URL. URL 不包含 HTTP 标头,因此无论您用于执行实际上传的 HTTP 客户端在将请求发送到生成的 URL 时都不会发送设置 header。

This simple answer is working for me.这个简单的答案对我有用。

val url = getS3Connection().,,generatePresignedUrl( "bucketname". "key", Date(Date().time + 1000 * 60 * 300) ) val url = getS3Connection().,generatePresignedUrl( "bucketname". "key", Date(Date().time + 1000 * 60 * 300) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM