-
Notifications
You must be signed in to change notification settings - Fork 165
/
s3.js
60 lines (57 loc) · 1.87 KB
/
s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
if (!process.env.S3_BUCKET || !process.env.S3_KEY || !process.env.S3_SECRET) {
console.log('To run this example, do this:')
console.log('npm install aws-sdk')
console.log('S3_BUCKET="(your s3 bucket)" S3_KEY="(your s3 key)" S3_SECRET="(your s3 secret) node examples/s3.js"');
process.exit(1);
}
var http = require('http');
var multiparty = require('../');
var AWS = require('aws-sdk');
var PORT = process.env.PORT || 27372;
var bucket = process.env.S3_BUCKET;
var s3Client = new AWS.S3({
accessKeyId: process.env.S3_KEY,
secretAccessKey: process.env.S3_SECRET
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
});
var server = http.createServer(function(req, res) {
if (req.url === '/') {
res.writeHead(200, { 'content-type': 'text/html' })
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="path" placeholder="s3 key here"><br>'+
'<input type="file" name="upload"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
} else if (req.url === '/upload') {
var form = new multiparty.Form();
var destPath;
form.on('field', function(name, value) {
if (name === 'path') {
destPath = value;
}
});
form.on('part', function(part) {
s3Client.putObject({
Bucket: bucket,
Key: destPath,
ACL: 'public-read',
Body: part,
ContentLength: part.byteCount
}, function(err, data) {
if (err) throw err;
console.log('done', data)
res.end('OK')
console.log('https://s3.amazonaws.com/' + bucket + '/' + destPath)
});
});
form.parse(req);
} else {
res.writeHead(404, { 'content-type': 'text/plain' })
res.end('404');
}
});
server.listen(PORT, function() {
console.info('listening on http://0.0.0.0:'+PORT+'/');
});