forked from kpantic/react-s3-uploader-multipart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3router.js
123 lines (106 loc) · 3.27 KB
/
s3router.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var uuidv4 = require('uuid/v4'),
aws = require('aws-sdk'),
express = require('express');
function checkTrailingSlash(path) {
if (path && path[path.length-1] != '/') {
path += '/';
}
return path;
}
function S3Router(options, middleware) {
if (!middleware) {
middleware = [];
}
var S3_BUCKET = options.bucket,
getFileKeyDir = options.getFileKeyDir || function() { return ""; };
if (!S3_BUCKET) {
throw new Error("S3_BUCKET is required.");
}
var getS3 = options.getS3;
if (!getS3) {
var s3Options = {};
if (options.region) {
s3Options.region = options.region;
}
if (options.signatureVersion) {
s3Options.signatureVersion = options.signatureVersion;
}
if (options.accessKeyId && options.secretAccessKey) {
s3Options.accessKeyId = options.accessKeyId;
s3Options.secretAccessKey = options.secretAccessKey;
}
getS3 = function() {
return new aws.S3(s3Options);
};
}
if (options.uniquePrefix === undefined) {
options.uniquePrefix = true;
}
var router = express.Router();
/**
* Redirects image requests with a temporary signed URL, giving access
* to GET an upload.
*/
function tempRedirect(req, res) {
var params = {
Bucket: S3_BUCKET,
Key: checkTrailingSlash(getFileKeyDir(req)) + req.params[0]
};
var s3 = getS3();
s3.getSignedUrl('getObject', params, function(err, url) {
res.redirect(url);
});
};
/**
* Image specific route.
*/
router.get(/\/img\/(.*)/, middleware, function(req, res) {
return tempRedirect(req, res);
});
/**
* Other file type(s) route.
*/
router.get(/\/uploads\/(.*)/, middleware, function(req, res) {
return tempRedirect(req, res);
});
/**
* Returns an object with `signedUrl` and `publicUrl` properties that
* give temporary access to PUT an object in an S3 bucket.
*/
router.get('/sign', middleware, function(req, res) {
var filename = (req.query.path || '') + (options.uniquePrefix ? uuidv4() + "_" : "") + req.query.objectName;
var mimeType = req.query.contentType;
var fileKey = checkTrailingSlash(getFileKeyDir(req)) + filename;
// Set any custom headers
if (options.headers) {
res.set(options.headers);
}
var s3 = getS3();
var params = {
Bucket: S3_BUCKET,
Key: fileKey,
Expires: 60,
ContentType: mimeType,
ACL: options.ACL || 'private'
};
s3.getSignedUrl('putObject', params, function(err, data) {
if (err) {
console.log(err);
return res.status(500).json({
error: {
code: 500,
message: "Cannot create S3 signed URL",
}
});
}
res.json({
signedUrl: data,
publicUrl: '/s3/uploads/' + filename,
filename: filename,
fileKey: fileKey,
});
});
});
return router;
}
module.exports = S3Router;