简体   繁体   English

AWS S3:我应该使用 POST 还是 PUT 请求来上传文件?

[英]AWS S3: should I use POST or PUT requests to upload a file?

I would like to know the pros and cons of using POST or PUT request to upload a file to Amazon Web Services S3.我想知道使用 POST 或 PUT 请求将文件上传到 Amazon Web Services S3 的优缺点。

I've already read some SO question like this one , but I would like to know the specific differences when using the AWS API.我已经读过像一些SO问题这一个,但我想使用AWS API时知道具体差异。

I managed to use both, but hardly see the difference.我设法同时使用了两者,但几乎看不出区别。 I am using both PUT and POST via AJAX and the XMLHTTPRequest object, to upload directly from the browser with a node.js backend generating the signature.我通过 AJAX 和 XMLHTTPRequest 对象使用 PUT 和 POST,直接从浏览器上传,node.js 后端生成签名。

The difference I noticed is that I can't restrict content-type and length server-side with PUT when I generate the signature, but this could be because I am just learning it now.我注意到的不同之处在于,当我生成签名时,我无法使用 PUT 限制服务器端的内容类型和长度,但这可能是因为我现在才刚刚学习它。

TO upload an object to an Amazon S3 bucket from an application, you should use the AWS SDK in a given programming language.要将对象从应用程序上传到 Amazon S3 存储桶,您应该使用给定编程语言的 AWS 开发工具包。 For example, if you are building an app using JavaScript, you can use the AWS SDK for JavaScript and use its client to upload an object.例如,如果您使用 JavaScript 构建应用程序,则可以使用适用于 JavaScript 的 AWS 开发工具包并使用其客户端上传对象。

Uploading photos to Amazon S3 from a browser 从浏览器上传照片到 Amazon S3

Likewise, if you are building a Spring BOOT app, then use the AWS SDK for Java V2 to upload an object.同样,如果您正在构建 Spring BOOT 应用程序,请使用适用于 Java V2 的 AWS 开发工具包上传对象。 An example is shown here:此处显示了一个示例:

Creating an example AWS photo analyzer application using the AWS SDK for Java 使用适用于 Java 的 AWS 开发工具包创建示例 AWS 照片分析器应用程序

Apart from the differences you noticed, when using POST, the response you get contains The object key name.除了您注意到的差异之外,在使用 POST 时,您得到的响应包含对象键名称。 This is useful if you upload multiple objects in parallel, to identify which upload succeeded or not in the callback.如果您并行上传多个对象,这将很有用,以在回调中确定哪个上传成功。

Also for access control, you can use POST and PUT differently (eg Not allowing object modification, while only allowing creation)同样对于访问控制,您可以不同地使用 POST 和 PUT(例如,不允许对象修改,而只允许创建)

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

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