6

我正在尝试生成客户端可用于将图像上传到特定 S3 存储桶的预签名 URL。我已成功生成对 GET 文件的请求,如下所示:

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

其中 expire 和 method 分别是 Date 和 HttpMethod 对象。

现在我正在尝试创建一个 URL 以允许用户 PUT 文件,但我不知道如何设置最大内容长度。我确实找到了有关POST 策略的信息,但我更喜欢在这里使用 PUT - 我也想避免构建 JSON,尽管这似乎不可能。

最后,另一种答案可能是将图像上传从 API Gateway 传递到 Lambda,这样我就可以在验证文件类型和大小后将其从 Lambda 上传到 S3(这并不理想)。

4

1 回答 1

0

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. 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)

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).

This is effectively a workaround rather than a solution, so I won't be accepting this answer.

于 2016-07-12T13:09:05.493 回答