I\'m trying to upload files to S3 service using Dropzone.js
I use this tutorial to upload the files directly from the client:
https://devcenter.heroku.com/articl
Here's what worked for my on the dropzone init parameters and node S3 signature on the backend:
HTML Frontend Code using Dropzone:
var myDropzone = new Dropzone(dropArea, {
url:"#",
dictDefaultMessage: "Drag n drop or tap here",
method: "PUT",
uploadMultiple: false,
paramName: "file",
maxFiles: 10,
thumbnailWidth: 80,
thumbnailHeight: 80,
parallelUploads: 20,
autoProcessQueue: true,
previewTemplate: dropPreviewTemplate,
//autoQueue: false, // Make sure the files aren't queued until manually added
previewsContainer: dropPreviewContainer, // Define the container to display the previews
clickable: true, //".fileinput-button" // Define the element that should be used as click trigger to select files.
accept: function(file, cb) {
//override the file name, to use the s3 signature
//console.log(file);
var params = {
fileName: file.name,
fileType: file.type,
};
//path to S3 signature
$.getJSON('/uploader', params).done(function(data) {
//console.log(data);
if (!data.signedRequest) {
return cb('Failed to receive an upload url');
}
file.signedRequest = data.signedRequest;
file.finalURL = data.downloadURL;
cb();
}).fail(function() {
return cb('Failed to receive an upload url');
});
},
sending: function(file, xhr) {
console.log('sending')
var _send = xhr.send;
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.send = function() {
_send.call(xhr, file);
}
},
processing:function(file){
this.options.url = file.signedRequest;
}
});
Here's the libraries I used on the node.js side
var Crypto = require("crypto"),
AWS = require("aws-sdk"),
Here's a sample of the CORS config on S3
*
PUT
*
Here's the code to generate the S3 Signature on node.js :
getPolicy:function(req,res)
{
var fileId = Crypto.randomBytes(20).toString('hex').toUpperCase();
var prefix = "bl_";
var newFileName = prefix+fileId;//req.query.fileName;
var s3 = new AWS.S3();
var s3_params = {
Bucket: BUCKET,
Key: newFileName,
Expires: 60,
ContentType: req.query.fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3_params, function(err, data){
if(err){
console.log(err);
}
else{
var return_data = {
signedRequest: data,
uploadURL: 'https://'+BUCKET+'.s3.amazonaws.com/'+newFileName,
downloadURL: 'http://'+BUCKET+'.s3-website-us-east-1.amazonaws.com/'+newFileName,
};
res.write(JSON.stringify(return_data));
res.end();
}
});
}
Hopefully some of this is helpful.