请帮忙将dropzone.js实现到Amazon S3服务器上传文件。已经查看了以下链接https://github.com/enyo/dropzone/issues/33,但是不知道如何实现,请帮忙实现。需要任何dropzone配置代码。
对于可能也遇到这个问题的人,我想分享我的工作场景(使用AWS Lambda实现无服务器)。
注意:可以在我的Vue S3 Dropzone组件中找到完整示例,与Dropzone和S3相关的代码实际上是框架无关的。
所以,基本上:
dropzone.processFile
。dropzone.options.url
。提示:
xhr.send
函数,否则Dropzone将始终在formData内发送文件,这对于PUT上传来说不好。// In the `accept` function we request a signed upload URL when a file being accepted
accept (file, done) {
lambda.getSignedURL(file)
.then((url) => {
file.uploadURL = url
done()
// And process each file immediately
setTimeout(() => dropzone.processFile(file))
})
.catch((err) => {
done('Failed to get an S3 signed upload URL', err)
})
}
// Set signed upload URL for each file being processing
dropzone.on('processing', (file) => {
dropzone.options.url = file.uploadURL
})
var AWS = require('aws-sdk')
var s3 = new AWS.S3();
// Make sure you set this env variable correctly
var bucketName = process.env.AWS_BUCKET_NAME
exports.handler = (event, context) => {
if (!event.hasOwnProperty('contentType')) {
context.fail({ err: 'Missing contentType' })
}
if (!event.hasOwnProperty('filePath')) {
context.fail({ err: 'Missing filePath' })
}
var params = {
Bucket: bucketName,
Key: event.filePath,
Expires: 3600,
ContentType: event.contentType
}
s3.getSignedUrl('putObject', params, (err, url) => {
if (err) {
context.fail({ err })
} else {
context.succeed({ url })
}
})
}