S3直接上传限制文件大小和类型文件大小、直接、类型、上传

2023-09-11 08:20:02 作者:薄樱鬼因一抹残笑

一个新手的问​​题,但我用Google搜索升技似乎无法找到任何解决方案。

A newbie question but I have googled abit and can't seem to find any solution.

我想允许用户直接文件先上传到S3,而不是通过我的服务器。通过这样做,有没有什么办法的文件,可以在实际上传到S3被检查大小限制和允许的类型? preferably不要用闪光灯,但JavaScript的。

I want to allow users to directly upload files to S3, not via my server first. By doing so, is there any way the files can be checked for size limit and permitted types before actually uploading to S3? Preferably not to use flash but javascript.

推荐答案

如果你在谈论安全问题(人上传大文件到你的水桶),是的,你可以限制文件大小与基于浏览器上传到S3。

If you are talking about security problem (people uploading huge file to your bucket), yes, You CAN restrict file size with browser-based upload to S3.

下面是政策变量,其中内容长度范围的关键点的一个例子。

Here is an example of the "policy" variable, where "content-length-range" is the key point.

"expiration": "'.date('Y-m-d\TG:i:s\Z', time()+10).'",
"conditions": [
    {"bucket": "xxx"},
    {"acl": "public-read"},
    ["starts-with","xxx",""],
    {"success_action_redirect": "xxx"},
    ["starts-with", "$Content-Type", "image/jpeg"],
    ["content-length-range", 0, 10485760]
]

在此情况下,如果uplaoding文件大小> 10mb的,上载请求将由亚马逊拒绝。

In this case, if the uplaoding file size > 10mb, the upload request will be rejected by Amazon.

当然,在开始上载过程之前,您应该使用JavaScript来检查文件的大小,并提出一些警告,如果它。

Of course, before starting the upload process, you should use javascript to check the file size and make some alerts if it does.

获得在JavaScript文件大小