1 | var S3FS = require('s3fs');
|
2 | var crypto = require('crypto');
|
3 | var path = require('path');
|
4 | var options = {};
|
5 | var s3fs;
|
6 | function S3Storage (opts) {
|
7 | if (!opts.bucket) throw new Error('bucket is required');
|
8 | if (!opts.secretAccessKey) throw new Error('secretAccessKey is required');
|
9 | if (!opts.accessKeyId) throw new Error('accessKeyId is required');
|
10 | if (!opts.region) throw new Error('region is required');
|
11 | if (!opts.dirname) throw new Error('dirname is required');
|
12 | options = opts;
|
13 | s3fs = new S3FS(options.bucket, options);
|
14 | }
|
15 |
|
16 | S3Storage.prototype._handleFile = function (req, file, cb) {
|
17 | var fileName = crypto.randomBytes(20).toString('hex');
|
18 | var outStream = s3fs.createWriteStream(options.dirname + '/' + fileName);
|
19 | file.stream.pipe(outStream);
|
20 | outStream.on('error', cb);
|
21 | outStream.on('finish', function(){
|
22 | cb(null, {size: outStream.bytesWritten, name: fileName})
|
23 | });
|
24 | }
|
25 |
|
26 | S3Storage.prototype._removeFile = function (req, file, cb) {
|
27 | s3fs.unlink(file.path, cb)
|
28 | }
|
29 |
|
30 | module.exports = function (opts) {
|
31 | return new S3Storage(opts)
|
32 | }
|