AWS S3预签名URL文件上传 – 在存储桶中保留文件名

huangapple go评论75阅读模式
英文:

AWS S3 Presigned URL file upload - Retain file name in bucket

问题

我正在尝试使用预签名的 PUT URL 将文件上传到 S3 存储桶,代码如下:

String tempFileKey = UUID.randomUUID().toString();
GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(tempBucketName, tempFileKey)
                        .withMethod(HttpMethod.PUT)
                        .withExpiration(expiration);
URL fileUrl = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

以上方法的问题是,如果用户上传名为 'xyz.jpg' 的文件,我无法在 S3 存储桶中保留文件名(或扩展名)。文件以 tempFileKey 作为名称保存。在创建预签名 URL 时,我不会知道文件名或扩展名。

我尝试使用 ${filename} 作为 tempFileKey,如 https://stackoverflow.com/questions/26386520/sending-file-direct-from-browser-to-s3-but-changing-file-name 中所建议的,但文件实际上只会以名称 ${filename} 的形式保存。

英文:

I am trying to upload files into S3 bucket using presigned PUT URL like below

String tempFileKey = UUID.randomUUID().toString();
    GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(tempBucketName, tempFileKey)
                            .withMethod(HttpMethod.PUT)
                            .withExpiration(expiration);
    URL fileUrl = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

The problem with above approach is if the user uploads a file named 'xyz.jpg', I am not able to retain the file name (or extension) in the s3 bucket. The file gets saved with the tempFileKey as name. I won't be knowing the file name or extension while creating the presigned url.

I tried using ${filename} as tempFilekey as suggested in https://stackoverflow.com/questions/26386520/sending-file-direct-from-browser-to-s3-but-changing-file-name but it is just getting saved under the name ${filename} (literally)

答案1

得分: 1

S3可以在签名密钥中插入${filename}来使用用户提供的文件名。

文档位于https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html

也许你的问题是PUT与POST的区别?

这里有一个示例,演示了在HTML表单中使用${filename}作为key:https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

英文:

S3 can use the user-provided filename if you put ${filename} in the signed key.

Docs are at https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html

Perhaps your problem is PUT vs POST?

There is a worked example here that uses this in an HTML form with ${filename} in the key: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

答案2

得分: 0

目标键是预签名 URL 的重要部分,并且是生成该 URL 的算法所必需的:

因此,您不能更改该键。

英文:

Object key is integral part of the pre-signed url and is required by the algorithm which generates the url:

Thus, you can't change the key.

huangapple
  • 本文由 发表于 2020年10月16日 08:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定