I am trying to upload an audio blob to amazon s3 , using my nodeJS. when i upload a blob audio from the client to S3 it works , but when i try to upload the audio blob through the nodeJS it doesn't upload the blob that is defined as file.
//The code in the client:
var fd = new FormData();
fd.append('fname', 'test.wav');
fd.append('data', recordedAudio);
$.ajax({
type: 'POST',
url: this.currentURL+'/sendRecordedAudioToServer',
data: fd,
processData: false,
headers: {key: key},
contentType: false
}).done(function(data) {
console.log(data);
});
In the server i use formidable to read the form that was sent to the server this is a lik to the github: https://github.com/felixge/node-formidable
The code in the server :
var AWS = require('aws-sdk');
var buffer;
AWS.config.update({
accessKeyId: 'xxxxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxxxxxxxxx'
});
.
.
.
.
var form = new formidable.IncomingForm(),
fields = [],
files = [];
form.on('error', function(err){
response.writeHead(200, {'content-type': 'text/plain'});
response.end('error:\n\n' + util.inspect(err));
});
form.on('field', function(field, value){
console.log(field, value);
fields.push([field, value]);
});
form.on('file', function(field, file){
console.log(field, file);
debugger;
buffer = file; //here i saved to audio blob , but now it's a file object
files.push([field, file]);
});
form.on('end', function(){
console.log('-> upload done');
var s3 = new AWS.S3();
var params = {
Bucket: '<myBucketName>',
Key: <MyKey> ,
Body: buffer
};
s3.putObject(params, function(err, data) {
if (err)
{
debugger;
console.log(err);
}
else
{
debugger;
console.log("Successfully uploaded data to s3");
}
res.writeHead(200, {'content-type': 'text/plain'});
res.write('Received fields \n ');
res.write('\n\n');
res.end('received files \n ');
});
});
form.encoding = 'utf-8';
form.parse(req);
another fact, i change the data that is beeing apploaded to s3 to a simple string, like 'hello world' , it will work, it seems that the problem is the format that my blob was transformed to (File) , and i didn't find any other way of sending my blob to the NodeJS SERVER that will keep it in a blob state...
please help me solve this issue , and if you don't have solution , maybe you can tell me how to not expose my secret keys in the client side, since i need to provide them