-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
345 lines (307 loc) · 14.1 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*jslint es5: true */
/*global
require, module, console
*/
(function () {
'use strict';
module.exports = function (config) {
// Set strict routing that /buckets/test/abc is not the same as /buckets/test/abc/
// adding the slash at the end denotes creating a folder and not a file
var router = require('express').Router({strict: true}),
AWS = require('aws-sdk'),
extend = require('util')._extend,
mime = require('mime-types'),
url = require('url'),
DEFAULT_MAX_KEYS = 1000,
// Return a location like object representing the request url
getLocation = function (req) {
var protocol = req.secure ? 'https://' : 'http://',
hostname = req.headers.host,
originalUrl = req.originalUrl !== '/' ? req.originalUrl : '',
fullUrl = protocol + hostname + originalUrl,
// Split out the query string
urlParts = fullUrl.split('?'),
origin = urlParts[0],
search = urlParts[1] ? '?' + urlParts[1] : '';
return {
// Protocol, hostname and port
origin: origin,
// Query string
search: search,
protocol: protocol,
hostname: hostname,
href: originalUrl
};
},
// Merge the two objects and create search query string with them
getMergedSearch = function (searchParams, otherParams) {
var mergedParams = extend(searchParams, otherParams),
paramKey,
searchString = '';
for (paramKey in mergedParams) {
if (mergedParams.hasOwnProperty(paramKey)) {
if (!searchString) {
searchString += '?' + paramKey + '=' + mergedParams[paramKey];
} else {
searchString += '&' + paramKey + '=' + mergedParams[paramKey];
}
}
}
return searchString;
},
getNextMarkerLink = function (req, nextMarker) {
var location = getLocation(req);
// Merge marker property with existing queries
location.search = getMergedSearch(req.query, {
marker: nextMarker
});
return location.origin + location.search;
},
getS3ObjectLink = function (req, bucket, content) {
var location = getLocation(req),
href = location.origin;
// If the objectKey is a folder we want to get a url to the bucket with a prefix param
if (content.Key.substr(-1) === '/' && content.Size === 0) {
// Merge marker property with existing queries
location.search = getMergedSearch(req.query, {
prefix: encodeURI(content.Key)
});
href += location.search;
} else {
// Since we are getting a link for an item the href must end with slash
if (href.substr(-1) !== '/') {
href += '/';
}
href += encodeURI(content.Key);
}
return href;
},
getBucketLink = function (req, bucket) {
var location = getLocation(req),
href = location.origin;
// Since we are getting a link for an item the href must end with slash
if (href.substr(-1) !== '/') {
href += '/';
}
// Add the bucket and search
href += bucket + location.search;
return href;
},
getOriginalink = function (req) {
var location = getLocation(req),
href = location.origin + location.search;
return href;
},
getFullKeyParam = function (params, route) {
// Due to how our routing accepts arbitrary length folders
// The key param could be split between params.key and params.'0'
// Return the combined keys
var restOfKey = params['0'] || '';
var key = params.key + restOfKey;
// Check if this route ends with a / ensure the key also does (similar to strict routing)
if (route.path.slice(-1) === '/' && key.slice(-1) !== '/') {
key = key + '/';
}
return key;
},
// Get all buckets
listBuckets = function (req, res) {
var s3 = new AWS.S3(),
handleListBuckets = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
// Add link
data.Buckets.forEach(function (bucket) {
bucket.links = [{
rel: 'self',
href: getBucketLink(req, bucket.Name)
}];
});
res.json(data.Buckets);
}
};
s3.listBuckets(handleListBuckets);
},
// Get a bucket
getBucket = function (req, res) {
var s3 = new AWS.S3(),
bucket = req.params.name,
location = getLocation(req),
params = {
Bucket: bucket,
Delimiter: req.query.delimiter,
Marker: req.query.marker,
MaxKeys: req.query.maxKeys || DEFAULT_MAX_KEYS,
Prefix: req.query.prefix
},
handleListObjects = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
// Get link for this set
data.links = [{
rel: 'self',
href: getOriginalink(req)
}];
// Map through bucket contents to filter out self reference and add links
data.Contents = data.Contents.map(function (content) {
// Add s3g link to reference the resource
content.links = [{
ref: 'self',
href: getS3ObjectLink(req, bucket, content)
}];
// Filter out any references to the parent file
if (location.origin + location.search !== content.links[0].href) {
return content;
}
}).filter(Boolean);
// If this set of objects has a next marker that means there are > 1000 so provide the link to the next set of data
if (data.IsTruncated) {
// Get link for next set using next marker if available or the last key in the content set
data.links.push({
rel: 'next',
href: getNextMarkerLink(req, data.NextMarker || data.Contents[data.Contents.length - 1].Key)
});
}
res.json(data);
}
};
s3.listObjects(params, handleListObjects);
},
// Create a new bucket
createBucket = function (req, res) {
var s3 = new AWS.S3(),
bucket = req.params.name,
params = {
Bucket: bucket
},
handleCreateBucket = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
// Add s3g link to reference the resource
data.links = [{
rel: 'self',
href: getOriginalink(req, bucket)
}];
res.json(data);
}
};
s3.createBucket(params, handleCreateBucket);
},
// Delete a bucket
deleteBucket = function (req, res) {
var s3 = new AWS.S3(),
params = {
Bucket: req.params.name
},
handleDeleteBucket = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
res.json(data);
}
};
s3.deleteBucket(params, handleDeleteBucket);
},
// Get an object key from the bucket
getObjectInBucket = function (req, res) {
var s3 = new AWS.S3(),
bucket = req.params.name,
key = getFullKeyParam(req.params, req.route),
params = {
Bucket: bucket,
Key: key
},
handleError = function (err) {
res.status(500).send(err);
};
s3.getObject(params).createReadStream().on('error', handleError).pipe(res);
},
// Upload a file to the bucket
createObjectInBucket = function (req, res) {
var s3 = new AWS.S3(),
bucket = req.params.name,
key = getFullKeyParam(req.params, req.route),
contentType = mime.lookup(key),
params = {
Bucket: bucket,
Key: key,
// Pipe the file through the request body
Body: req
},
trackUploadProgress = function (evt) {
// This function is called each time a chunk of the file is uploaded
//console.log(evt);
},
handleUploadComplete = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
// Add s3g link to reference the resource
data.links = [{
rel: 'self',
href: getOriginalink(req)
}];
res.json(data);
}
};
// Set content type for object if we have one
if (contentType) {
params.ContentType = contentType;
}
s3.upload(params).on('httpUploadProgress', trackUploadProgress).send(handleUploadComplete);
},
// Delete objects from a bucket
deleteObjectsInBucket = function (req, res) {
var s3 = new AWS.S3(),
bucket = req.params.name,
key = getFullKeyParam(req.params, req.route),
params = {
Bucket: bucket,
Delete: {
Objects: [{
Key: key
}]
}
},
handleDeleteObjects = function (err, data) {
if (err) {
res.status(500).send(err);
} else {
res.json(data);
}
};
s3.deleteObjects(params, handleDeleteObjects);
};
// If there are config options specified update the config with them
if (config) {
AWS.config.update(config);
}
// Routes
// Get all buckets on root url
router.get('/', listBuckets);
// Bucket CRUD
// Get a bucket by name
router.get('/:name', getBucket);
router.get('/:name/', getBucket);
// Create a bucket by name
router.put('/:name', createBucket);
router.put('/:name/', createBucket);
// Delete a bucket by name
router.delete('/:name', deleteBucket);
router.delete('/:name/', deleteBucket);
// Object CRUD
// Get an object(key) from bucket(name)
router.get('/:name/:key*', getObjectInBucket);
router.get('/:name/:key/', getObjectInBucket);
// Create an object(key) from bucket(name)
router.put('/:name/:key*', createObjectInBucket);
router.put('/:name/:key/', createObjectInBucket);
// Delete an object(key) from bucket(name)
router.delete('/:name/:key*', deleteObjectsInBucket);
router.delete('/:name/:key/', deleteObjectsInBucket);
return router;
};
}());