[英]How to restrict the size of the file being uploaded on to the AWS S3 service using presigned urls
I'm using an AWS S3 presigned url to upload picture from a client (mobile app).我正在使用预签名为 url 的 AWS S3 从客户端(移动应用程序)上传图片。 I want to prevent the user to upload large files.我想阻止用户上传大文件。 Is there a way to limit the file size of an uploaded file?有没有办法限制上传文件的文件大小?
Thanks谢谢
Check out "content-length-range" in s3 policy.查看 s3 策略中的“内容长度范围”。 http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
The conditions in a POST policy is an array of objects, each of which is used to validate the contents of the uploaded object. POST 策略中的条件是一组对象,每个对象都用于验证上传对象的内容。 You can use these conditions to restrict what is allowed in the request.您可以使用这些条件来限制请求中允许的内容。 Each form field that you specify in a form (except x-amz-signature, file, policy, and field names that have an x-ignore- prefix) must appear in the list of conditions.您在表单中指定的每个表单字段(x-amz-signature、文件、策略和具有 x-ignore- 前缀的字段名称除外)都必须出现在条件列表中。
content-length-range内容长度范围
The minimum and maximum allowable size for the uploaded content.上传内容的最小和最大允许大小。 This condition supports content-length-range condition match type.此条件支持内容长度范围条件匹配类型。
I have solved this problem perfectly, Though, the POST policy can work.我已经完美地解决了这个问题,虽然,POST 策略可以工作。 but use presigned urls is more comfortable.但是使用预签名的 url 更舒服。
I use nodejs as backend.我使用nodejs作为后端。 And use getSignedUrl to limit the size of upload file.并使用getSignedUrl来限制上传文件的大小。
It just need modify three line of aws-sdk code.只需修改三行aws-sdk代码即可。 And I have tried, it can work very well.我已经尝试过,它可以很好地工作。
I don't know why aws-sdk of nodejs don't support it.不知道为什么nodejs的aws-sdk不支持。 If there is some reason, please share to me.如果有什么原因,请分享给我。
first, open "node_modules/aws-sdk/lib/signers/v4.js", comment the following two line首先,打开“node_modules/aws-sdk/lib/signers/v4.js”,注释下面两行
second, open "node_modules/aws-sdk/lib/services/s3.js", comment the following one line其次,打开“node_modules/aws-sdk/lib/services/s3.js”,注释下面一行
third, write the code to generate presigned url and upload file三、编写代码生成预签名url并上传文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.