AWS SDK提供了一个功能,可以使用预签名URL在Amazon S3上上传文件。这里提供了一个解决多文件上传的预签名URL的方法:
var s3 = new AWS.S3();
var params = {
Bucket: 'my-bucket',
Key: 'my-key',
ContentType: 'image/jpeg',
ACL: 'public-read',
Expires: 60,
Fields: {
key: 'uploads/${filename}',
acl: 'public-read',
success_action_redirect: 'url-to-redirect-to-when-successful'
},
Conditions: [
{acl: 'public-read'},
{'Content-Type': 'image/jpeg'},
['content-length-range', 100, 10000000]
]
};
s3.createPresignedPost(params, function(err, data) {
if (err) {
console.error('Presigning post data encountered an error', err);
} else {
console.log('The post data is', data);
}
});
这将在60秒内创建一个预签名URL,并生成一个带有必要字段的对象,以便进行文件上传。
首先,我们需要控制文件输入。通过在HTML中添加多个文件输入或在JavaScript中动态添加,我们可以允许多个文件进行上传。例如,我们可以在HTML页面中添加以下内容: