简体   繁体   English

为Amazon S3生成具有最大内容长度的预签名PUT URL

[英]Generating a pre-signed PUT url for Amazon S3 with a maximum content-length

I'm trying to generate a pre-signed URL a client can use to upload an image to a specific S3 bucket. 我正在尝试生成客户端可以用来将图像上传到特定S3存储桶的预签名URL。 I've succesfully generated requests to GET files, like so: 我已经成功生成了对GET文件的请求,如下所示:

GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucket, filename);
urlRequest.setMethod(method);
urlRequest.setExpiration(expiration);

where expiration and method are Date and HttpMethod objects respectively. 其中expiration和method分别是Date和HttpMethod对象。

Now I'm trying to create a URL to allow users to PUT a file, but I can't figure out how to set the maximum content-length. 现在,我试图创建一个URL,以允许用户放置文件,但是我不知道如何设置最大内容长度。 I did find information on POST policies , but I'd prefer to use PUT here - I'd also like to avoid constructing the JSON, though that doesn't seem possible. 我确实找到了有关POST政策的信息,但是我更喜欢在这里使用PUT-我也想避免构造JSON,尽管这似乎不太可能。

Lastly, an alternative answer could be some way to pass an image upload from the API Gateway to Lambda so I can upload it from Lambda to S3 after validating file type and size (which isn't ideal). 最后,替代的答案可能是将图像上传从API网关传递到Lambda的某种方法,因此我可以在验证文件类型和大小后将其从Lambda上传到S3(这不理想)。

While I haven't managed to limit the file size on upload, I ended up creating a Lambda function that is activated on upload to a temporary bucket. 虽然我没有设法限制上传时的文件大小,但最终创建了一个Lambda函数,该函数在上传到临时存储区时被激活。 The function has a signature like the below 该函数具有如下所示的签名

public static void checkUpload(S3EventNotification event) {

(this is notable because all the guides I found online refer to a S3Event class that doesn't seem to exist anymore) (这是值得注意的,因为我在网上找到的所有指南都引用了似乎不再存在的S3Event类)

The function pulls the file's metadata (not the file itself, as that potentially counts as a large download) and checks the file size. 该函数提取文件的元数据(而不是文件本身,因为这可能算作大量下载),并检查文件大小。 If it's acceptable, it downloads the file then uploads it to the destination bucket. 如果可以接受,它将下载文件,然后将其上传到目标存储桶。 If not, it simply deletes the file. 如果没有,它只是删除文件。

This is far from ideal, as uploads failing to meet the criteria will seem to work but then simply never show up (as S3 will issue a 200 status code on upload without caring what Lambda's response is). 这远非理想,因为不符合条件的上载似乎可以正常工作,但随后根本就不会出现(因为S3会在上载时发出200状态代码,而不关心Lambda的响应是什么)。

This is effectively a workaround rather than a solution, so I won't be accepting this answer. 这实际上是一种解决方法,而不是解决方案,所以我不会接受这个答案。

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

相关问题 为带有空格的文件生成预签名的 S3 URL - Generating pre-signed S3 URL for file with spaces S3:为给定密钥生成预签名URL。 [密钥可能存在/不存在] - S3: Generating Pre-Signed URL for a given Key. [Key may/not present] RestTemplate无法与S3预签名放置URL一起使用 - RestTemplate not working with S3 pre-signed put URLs 使用预签名 URL 限制 S3 PUT 文件大小 - Limiting the S3 PUT file size using pre-signed URLs 使用Retrofit2将文件上载到AWS S3预签名URL - Upload a file to AWS S3 pre-signed URL using Retrofit2 使用预先签名的URL上传到带有curl的s3(获得403) - Upload to s3 with curl using pre-signed URL (getting 403) 无法播放使用预签名 URL 上传到 AWS S3 的 mp4 视频 - Unable to play mp4 video uploaded to AWS S3 using pre-signed URL 使用CannedAccessControlList.Private的generatePresignedUrlRequest时,S3预签名URL文件上载不起作用 - S3 pre-signed URL file upload not working when generatePresignedUrlRequest with CannedAccessControlList.Private AWS S3 GET 预签名 url 因 CORS 间歇性失败 - AWS S3 GET pre-signed url fails with CORS intermittently 我可以使用 OkHttp3 客户端从 Amazon S3 Presigned URL 获取 header 详细信息“Content-Length”吗? - Can I get the header detail “Content-Length” from an Amazon S3 Presigned URL using OkHttp3 client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM