Skip to content

Commit

Permalink
[WiP] stream files directly to S3 #35
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 18, 2016
1 parent b9ba8bd commit cb396de
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Binary file added examples/stream/awesomo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions examples/stream/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require('env2')('./.env');

var AWS = require('aws-sdk');
AWS.config.region = process.env.AWS_REGION;

var fs = require('fs');
var mime = require('mime-types');
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({port: Number(process.argv[2] || 8080) }); // tell hapi which TCP Port to "listen" on



server.route({
method: 'POST',
path: '/submit',
config: {

payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data'
},

handler: function (request, reply) {
var data = request.payload;
if (data.file) {

var name = data.file.hapi.filename;
// var path = __dirname + "/uploads/" + name;
// var file = fs.createWriteStream(path);

// file.on('error', function (err) {
// console.error(err)
// });
//
// data.file.pipe(file);
//
// data.file.on('end', function (err) {
// var ret = {
// filename: data.file.hapi.filename,
// headers: data.file.hapi.headers
// }
// reply(JSON.stringify(ret));
// });
var s3Bucket = new AWS.S3({
params: {
Bucket: process.env.AWS_S3_BUCKET,
ACL: 'public-read',
Key: name,
ContentType: mime.lookup(name)
}
});

s3Bucket.upload({Body: data.file._data })
.on('httpUploadProgress', function(evt) { console.log(evt); })
.send(function(err, data) {
console.log(err, data);
request.handleError(err, data);
return reply(JSON.stringify(data));
});

}

}
}
});

server.start(function () {
console.log('info', 'Server running at: ' + server.info.uri);
});
Empty file added examples/stream/test.js
Empty file.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"aws-sdk": "^2.6.5",
"env2": "^2.1.1",
"hapi": "^15.0.3",
"hapi-error": "^1.4.1",
"inert": "^4.0.2"
"hapi-error": "^1.5.0",
"inert": "^4.0.2",
"mime-types": "^2.1.12"
},
"devDependencies": {
"codecov": "^1.0.1",
Expand Down

0 comments on commit cb396de

Please sign in to comment.