-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (55 loc) · 1.57 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
/* eslint-disable global-require */
/*
node-storage-wrapper
*/
// load node utils
const AWS = require('aws-sdk')
const fs = require('fs')
const { Storage } = require('@google-cloud/storage')
// create wrapper
function StorageWrapper(config) {
// check config
if (!config || !config.gs) return Promise.reject(new Error('storage config invalid'))
// enable SDKs
this.sdk = {}
// load node utils
this.sdk.fs = fs
// load google cloud storage sdk
this.sdk.gs = new Storage(config.gs)
// load aws sdk
if (config.s3) {
this.sdk.s3 = new AWS.S3(config.s3)
}
// configure logging
this.config = {
logging: config.logging,
}
// set logging
this.sdk.log = (that, level, message) => {
let thisMessage
if (message instanceof Array) thisMessage = message.join(' ')
else thisMessage = message
if (level === 'log' && that.config.logging) console.log(thisMessage)
else if (level === 'warn' && that.config.logging) console.warn(thisMessage)
else if (level === 'error' && that.config.logging) console.error(thisMessage)
}
// import functions
this.createUri = require('./createUri')
this.createUrl = require('./createUrl')
this.delete = require('./delete')
this.list = require('./list')
this.load = require('./load')
this.move = require('./move')
this.save = require('./save')
// log progress
this.sdk.log(this, 'log', [
'storage.index',
'loaded config',
JSON.stringify({
gs: config.gs ? config.gs.projectId : null,
s3: config.s3 ? { accessKeyId: config.s3.accessKeyId, region: config.s3.region } : null,
}),
])
}
// export
module.exports = StorageWrapper