-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WiP] stream files directly to S3 #35
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters