简体   繁体   English

如何使用预签名 url 限制上传到 AWS S3 服务的文件的大小

[英]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.

相关问题 通过 S3 的预签名 URL 限制上传文件的大小 - Limitting size of uploaded file via presigned URLs to S3 如何限制用户下载上传到aws s3的文件 - How to restrict users from download files uploaded to aws s3 AWS Lambda 使用调用的凭证为 S3 创建预签名的 URL - AWS Lambda create presigned URL for S3 using invoked credentials AWS S3 预签名 url 上传返回 200 但文件不在使用 NodeJS 和节点获取的存储桶中 - AWS S3 presigned url upload returns 200 but the file is not in the bucket using NodeJS & node-fetch 使用 wget --spider 检查 AWS S3 预签名链接是否存在 - Checking if AWS S3 presigned link exists using wget --spider 使用预签名 URL“SignatureDoesNotMatch”获取 AWS S3 Object - Fetching AWS S3 Object using Presigned URL "SignatureDoesNotMatch" 如何使用预签名的 URL 以及 s3 aws-sdk-ruby v3 中的标签上传 object - How to upload an object using presigned URL along with tags in s3 aws-sdk-ruby v3 如何使用 postdata preSigned Url 调用 Amazon S3 存储桶以使用空手道上传文件 - How to call Amazon S3 bucket using postdata preSigned Url to upload a file using Karate 带有星号的 AWS S3 预签名 url 问题 - AWS S3 presigned url issue with asterisk 如何使用预签名 url 在同一订单中为 Amazon S3 上传文件使用表单字段 - How to use form fields in the same order for Amazon S3 upload file using presigned url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM