forked from RedbirdHQ/serverless-s3-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (102 loc) · 3.41 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
const Promise = require('bluebird');
const S3rver = require('s3rver');
const fs = require('fs-extra'); // Using fs-extra to ensure destination directory exist
const AWS = require('aws-sdk');
class ServerlessS3Local {
constructor(serverless, options) {
this.serverless = serverless;
this.service = serverless.service;
this.options = options;
this.provider = 'aws';
this.client = null;
this.commands = {
s3: {
commands: {
start: {
usage: 'Start S3 local server.',
lifecycleEvents: ['startHandler'],
options: {
port: {
shortcut: 'p',
usage: 'The port number that S3 will use to communicate with your application. If you do not specify this option, the default port is 4569',
},
directory: {
shortcut: 'd',
usage: 'The directory where S3 will store its objects. If you do not specify this option, the file will be written to the current directory.',
},
buckets: {
shortcut: 'b',
usage: 'After starting S3 local, create specified buckets',
},
cors: {
shortcut: 'c',
usage: 'Enable CORS',
},
},
},
},
},
};
this.hooks = {
's3:start:startHandler': this.startHandler.bind(this),
'before:offline:start:init': this.startHandler.bind(this),
'before:offline:start': this.startHandler.bind(this),
'before:offline:start:end': this.endHandler.bind(this),
};
}
startHandler() {
return new Promise((resolve) => {
const config = (this.serverless.service.custom && this.serverless.service.custom.s3) || {};
const options = Object.assign({}, this.options, config);
// Mix buckets from resources list and buckets from parameters
const buckets = this.buckets().concat(options.buckets) || [];
const port = options.port || 4569;
const hostname = 'localhost';
const silent = false;
const cors = options.cors || false;
const dirPath = options.directory || './buckets';
fs.ensureDirSync(dirPath); // Create destination directory if not exist
const directory = fs.realpathSync(dirPath);
this.client = new S3rver({
port,
hostname,
silent,
directory,
cors,
}).run((err, s3Host, s3Port) => {
if (err) {
console.error('Error occured while starting S3 local.');
return;
}
console.log(`S3 local started ( port:${s3Port} )`);
const s3Client = new AWS.S3({
s3ForcePathStyle: true,
endpoint: new AWS.Endpoint(`http://localhost:${s3Port}`),
});
buckets.forEach((bucket) => {
s3Client.createBucket({ Bucket: bucket }, () => {});
});
});
resolve();
});
}
endHandler() {
this.client.close();
console.log('S3 local closed');
}
/**
* Get bucket list from serveless.yml resources
*
* @return {object} Array of bucket name
*/
buckets() {
const resources = this.service.resources.Resources;
return Object.keys(resources).map((key) => {
if (resources[key].Type === 'AWS::S3::Bucket') {
return resources[key].Properties.BucketName;
}
return null;
}).filter(n => n);
}
}
module.exports = ServerlessS3Local;