[英]Set random file name when upload file to AWS S3 with pre-signed POST url
I use signed POST url with jQuery-file-upload to upload image to AWS S3 directly. 我将签名的POST网址与jQuery-file-upload结合使用,以将图像直接上传到AWS S3。
It work fine on my PC, but when I test with iPhone, I fine out that iOS will upload any picture with same name like image.png
or image.jpeg
. 它在我的电脑上工作正常,但是当我用iPhone测试时,我很清楚iOS会上传任何名称相同的图片,如image.png
或image.jpeg
。
That make the image upload finished last replace any image upload successed before it. 这使得图像上传完成最后替换之前成功的任何图像上传。
I generate presigned post with boto3 in Python3 like this: 我在Python3中使用boto3生成了预先签名的帖子,如下所示:
post = s3.generate_presigned_post(
Bucket='?????????',
Key=get_random_string(8,'simple') + '/${filename}',
Fields={
'success_action_status': '201',
'acl': 'public-read'
},
Conditions=[{
'success_action_status': '201'
},
{
'acl': 'public-read'
},
["starts-with", "$Content-Type", ""]
]
)
post['fields'] = json.dumps(post['fields'])
And here is my javascript code: 这是我的javascript代码:
$(function () {
'use strict';
var form = $('#fileupload');
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
dropZone: $('#dropzone'),
previewMaxHeight: 300,
previewMaxWidth: 300,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
dataType: 'XML',
getFilesFromResponse: function (data) {
var key = $(data.jqXHR.responseXML).find("Key").text();
var url = $(data.jqXHR.responseXML).find("Location").text();
return [{
url: url,
name: key,
thumbnailUrl: url,
}];
},
}).on('fileuploadsubmit', function (e, data) {
data.formData = (function (form) {
var r = form.serializeArray();
$.each(form.data('theform-data'), function(k, v) {
r.push({name: k , value: v})
});
return r;
})(data.form);
data.formData.push({name: "Content-Type" , value: data.files[0].type});
});
});
The get_random_string(8,'simple')
in Python just can prevent same file name in different upload, but it cannot prevent same file name when user upload multifile at the same time (because they upload with same pre-signed POST url). Python中的get_random_string(8,'simple')
只能在不同的上传中阻止相同的文件名,但是当用户同时上传多文件时(因为它们使用相同的预签名POST URL进行上传),它不能阻止相同的文件名。
So I wonder if there has any way that I can set random file name when upload file to AWS S3 with pre-signed POST url? 因此,我想知道在使用预签名的POST URL将文件上传到AWS S3时,是否可以设置随机文件名?
I found out that this would work: 我发现这可行:
$(function () {
'use strict';
var form = $('#fileupload');
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
dropZone: $('#dropzone'),
previewMaxHeight: 300,
previewMaxWidth: 300,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
dataType: 'XML',
getFilesFromResponse: function (data) {
var key = $(data.jqXHR.responseXML).find("Key").text();
var url = $(data.jqXHR.responseXML).find("Location").text();
return [{
url: url,
name: key,
thumbnailUrl: url,
}];
},
}).on('fileuploadsubmit', function (e, data) {
data.formData = (function (form) {
var r = form.serializeArray();
$.each(form.data('theform-data'), function(k, v) {
if (k == "key") {
var filename = v.split("/");
var random_prefix = filename[0];
var ext = data.files[0].name.split(".").slice(-1)[0];
v = random_prefix + '/'
+ (function (length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
})(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
+ '.' + ext.toLowerCase();
};
r.push({name: k , value: v})
});
return r;
})(data.form);
data.formData.push({name: "Content-Type" , value: data.files[0].type});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.