-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
s3.js
460 lines (387 loc) · 10.6 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
var S3 = require('aws-sdk/clients/s3')
var thunky = require('thunky')
var from = require('from2')
var mime = require('mime')
var each = require('stream-each')
var stream = require('readable-stream')
var duplexify = require('duplexify')
var bulk = require('bulk-write-stream')
module.exports = S3Storage
function S3Storage (bucket, opts) {
if (!(this instanceof S3Storage)) return new S3Storage(bucket, opts)
if (!opts) opts = {}
this.bucket = bucket.replace(/^s3:\/\//, '')
this.region = opts.region || 'us-east-1' // default aws region
this.s3 = new S3(opts)
this.ready = thunky(this._open.bind(this))
this.prefix = opts.prefix || null
}
S3Storage.prototype.versions = function (key, cb) {
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.s3.listObjectVersions({
Bucket: self.bucket,
Prefix: join(self.prefix, key)
}, function (err, res) {
if (err) return cb(err)
var v = res.Versions.map(function (v) {
return {
key: v.Key.replace(self.prefix + '/', ''),
latest: v.IsLatest,
version: v.VersionId,
modified: v.LastModified
}
})
cb(null, v)
})
})
}
S3Storage.prototype.list =
S3Storage.prototype.createListStream = function (opts) {
if (!opts) opts = {}
var self = this
var marker = join(self.prefix, opts.marker || null)
var limit = opts.limit || Infinity
var stream = from.obj(read)
var open = false
return stream
function openAndRead (size, cb) {
open = true
self.ready(function (err) {
if (err) return cb(err)
read(size, cb)
})
}
function read (size, cb) {
if (!open) return openAndRead(size, cb)
self.s3.listObjects({
Bucket: self.bucket,
Prefix: join(self.prefix, opts.prefix || null),
Marker: marker
}, function (err, res) {
if (err) return cb(err)
var contents = res.Contents
var len = Math.min(contents.length, limit)
if (!len) return cb(null, null)
for (var i = 0; i < len; i++) {
var c = contents[i]
var next = {key: c.Key.replace(self.prefix + '/', ''), size: c.Size, modified: c.LastModified}
limit--
marker = c.Key
if (i < len - 1) stream.push(next)
else cb(null, next)
}
})
}
}
S3Storage.prototype.rename = function (from, to, cb) {
var self = this
var stream = this.list({prefix: from})
each(stream, ondata, cb)
function ondata (data, next) {
var key = data.key
self.s3.copyObject({
Bucket: self.bucket,
CopySource: self.bucket + '/' + join(self.prefix, key),
Key: join(self.prefix, key.replace(from, to)) // from is *always* in the beginning of the key
}, function (err) {
if (err) return next(err)
self.del(key, next)
})
}
}
S3Storage.prototype.del = function (key, opts, cb) {
if (typeof opts === 'function') return this.del(key, null, opts)
var v = opts && opts.version
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.s3.deleteObject({
Bucket: self.bucket,
Key: join(self.prefix, key),
VersionId: v
}, cb)
})
}
S3Storage.prototype.delBatch = function (objects, cb) {
if (!cb) cb = noop
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.s3.deleteObjects({
Bucket: self.bucket,
Delete: {
Objects: objects.map(function (obj) {
return {
Key: join(self.prefix, obj.key),
VersionId: obj.version
}
})
}
}, cb)
})
}
S3Storage.prototype.exists = function (key, opts, cb) {
if (typeof opts === 'function') return this.exists(key, null, opts)
this.stat(key, opts, function (err, st) {
if (err && err.code === 'NotFound') return cb(null, false)
if (err) return cb(err, false)
cb(null, true)
})
}
S3Storage.prototype.stat = function (key, opts, cb) {
if (typeof opts === 'function') return this.stat(key, null, opts)
var v = opts && opts.version
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.s3.headObject({
Bucket: self.bucket,
Key: join(self.prefix, key),
VersionId: v
}, function (err, data) {
if (err) return cb(err)
cb(null, {
size: data.ContentLength,
modified: new Date(data.LastModified)
})
})
})
}
S3Storage.prototype.createReadStream = function (key, opts) {
var proxy = duplexify()
var self = this
var v = opts && opts.version
var Range = (opts && (typeof opts.start === 'number' || typeof opts.end === 'number'))
? 'bytes=' + (opts.start || 0) + '-' + (typeof opts.end === 'number' ? opts.end : '')
: undefined
proxy.setWritable(false)
this.ready(function (err) {
if (err) return proxy.destroy(err)
proxy.setReadable(self.s3.getObject({
Bucket: self.bucket,
Key: join(self.prefix, key),
VersionId: v,
Range
}).createReadStream())
})
return proxy
}
S3Storage.prototype.createMultipartStream = function (key, opts) {
if (typeof opts === 'number') opts = {length: opts}
if (!opts) opts = {}
var self = this
var pending = null
var batchSize = opts.batchSize || 4 * 1024 * 1024 * 1024
var lastSize = (opts.length % batchSize) || batchSize
var ws = bulk(write, flush)
var ondrain = null
var onflush = null
var flushed = false
var proxy = null
var missing = 0
var uploadId = null
var part = 1
var parts = Math.ceil(opts.length / batchSize)
var uploadedParts = new Array(parts)
var completed = 0
var s3Key = join(self.prefix, key)
this.ready(function (err) {
if (err) return ws.destroy(err)
self.s3.createMultipartUpload({
Bucket: self.bucket,
Key: s3Key
}, function (err, res) {
if (err) return ws.destroy(err)
uploadId = res.UploadId
if (ws.destroyed) return cleanup()
if (pending) write(pending[0], pending[1])
})
})
ws.on('close', function () {
if (completed !== parts) cleanup()
})
return ws
function cleanup () {
if (!uploadId) return
self.s3.abortMultipartUpload({
UploadId: uploadId,
Bucket: self.bucket,
Key: s3Key
}, function () {
// ... do nothing
})
}
function write (data, cb) {
if (!uploadId) {
pending = [data, cb]
return
}
var drained = true
for (var i = 0; i < data.length; i++) {
var d = data[i]
if (!missing) {
proxy = new stream.Readable({ read })
missing = addPart(proxy)
if (!missing) return cb(new Error('Writing too much'))
}
if (d.length < missing) {
drained = proxy.push(d)
missing -= d.length
} else if (d.length === missing) {
proxy.push(d)
missing = 0
proxy.push(null)
drained = true
} else {
proxy.push(d.slice(0, missing))
data[i--] = d.slice(missing)
missing = 0
proxy.push(null)
drained = true
}
}
if (!drained) ondrain = cb
else cb()
}
function flush (cb) {
if (flushed) return cb()
onflush = cb
}
function read () {
if (!ondrain) return
var cb = ondrain
ondrain = null
cb()
}
function addPart (body) {
var len = part === parts ? lastSize : batchSize
if (part > parts) return 0
var PartNumber = part++
self.s3.uploadPart({
UploadId: uploadId,
Bucket: self.bucket,
Key: s3Key,
Body: body,
PartNumber,
ContentLength: len
}, function (err, res) {
if (err) return ws.destroy(err)
uploadedParts[PartNumber - 1] = {
ETag: res.ETag,
PartNumber
}
if (++completed !== parts) return
self.s3.completeMultipartUpload({
UploadId: uploadId,
Bucket: self.bucket,
Key: s3Key,
MultipartUpload: {
Parts: uploadedParts
}
}, function (err) {
if (err) return ws.destroy(err)
flushed = true
if (onflush) onflush()
})
})
return len
}
}
S3Storage.prototype.createWriteStream = function (key, opts) {
if (typeof opts === 'number') opts = {length: opts}
if (!opts) opts = {}
if (opts.length > 4 * 1024 * 1024 * 1024) return this.createMultipartStream(key, opts)
var self = this
var ondrain = null
var onflush = null
var flushed = false
var proxy = new stream.Readable({read: read})
var ws = bulk(write, flush)
this.ready(function (err) {
if (err) {
proxy.push(null)
ws.destroy(err)
return
}
self.s3.putObject({
Bucket: self.bucket,
Key: join(self.prefix, key),
ContentLength: opts.length,
Body: proxy
}, function (err) {
if (err) return ws.destroy(err)
flushed = true
if (onflush) onflush()
})
})
return ws
function read () {
if (!ondrain) return
var cb = ondrain
ondrain = null
cb()
}
function write (data, cb) {
var drained = true
for (var i = 0; i < data.length; i++) drained = proxy.push(data[i])
if (!drained) ondrain = cb
else cb()
}
function flush (cb) {
if (flushed) return cb()
onflush = cb
}
}
S3Storage.prototype.put = function (key, buf, meta, cb) {
if (typeof meta === 'function') {
cb = meta
meta = undefined
}
if (!cb) cb = noop
var self = this
var type = mime.getType(key)
this.ready(function (err) {
if (err) return cb(err)
self.s3.putObject({
Bucket: self.bucket,
ContentType: type,
Key: join(self.prefix, key),
Body: buf,
Metadata: meta
}, cb)
})
}
S3Storage.prototype.get = function (key, opts, cb) {
if (typeof opts === 'function') return this.get(key, null, opts)
if (!cb) cb = noop
var v = opts && opts.version
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.s3.getObject({
Bucket: self.bucket,
Key: join(self.prefix, key),
VersionId: v
}, function (err, data) {
if (err) return cb(err)
cb(null, data.Body, data.Metadata)
})
})
}
S3Storage.prototype._open = function (cb) {
var opts = this.region === 'us-east-1'
? {Bucket: this.bucket}
: {Bucket: this.bucket, CreateBucketConfiguration: {LocationConstraint: this.region}}
this.s3.createBucket(opts, function (err) {
// if we get a 409 it simply means we already created the bucket
// if we get a 403 it is access denied, but thats prob because the AWS user does not have that perm
if (err && err.statusCode !== 409 && err.statusCode !== 403) return cb(err)
cb(null)
})
}
function noop () {}
function join (...args) {
return args.filter(s => s != null).join('/').replace(/\/{2,}/g, '/')
}