-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bf421d
commit c8dec56
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* `s3s-service.js` | ||
* - common S3 services. | ||
* | ||
* | ||
* @author Steve <[email protected]> | ||
* @date 2019-07-19 initial version | ||
* | ||
* @copyright (C) lemoncloud.io 2019 - All Rights Reserved. | ||
*/ | ||
module.exports = function(_$, name, options) { | ||
'use strict'; | ||
name = name || 'S3S'; // engine service name. | ||
|
||
// core module | ||
const $U = _$.U; | ||
if (!$U) throw new Error('$U is required!'); | ||
|
||
//! load common(log) functions | ||
const _log = _$.log; | ||
const _inf = _$.inf; | ||
const _err = _$.err; | ||
|
||
// NAMESPACE TO BE PRINTED. | ||
const NS = $U.NS(name); | ||
|
||
//! external service | ||
const $aws = function() { | ||
if (!_$.aws) throw new Error('$aws is required!'); | ||
return _$.aws; | ||
}; | ||
|
||
/** **************************************************************************************************************** | ||
* Public Common Interface Exported. | ||
** ****************************************************************************************************************/ | ||
//TODO - load via environ. | ||
const region = 'ap-northeast-2'; | ||
|
||
/** | ||
* hello | ||
*/ | ||
const hello = () => { | ||
return { | ||
hello: 's3-service', | ||
}; | ||
}; | ||
|
||
//! get aws client for S3 | ||
const instance = () => { | ||
const AWS = $aws(); | ||
const config = { region }; | ||
return new AWS.S3(config); // SQS Instance. shared one??? | ||
}; | ||
|
||
//! translate to real s3 arn by id. | ||
const bucketId = () => { | ||
//TODO - use `env/config#bucket` configuration. | ||
const name = 'lemon-hello-www'; | ||
return `${name}`; | ||
}; | ||
|
||
/** | ||
* upload a file to S3 Bucket | ||
* | ||
* @param {string} bucketId | ||
* @param {string} fileName | ||
* @param {string} fileStream | ||
* @param {object} tags (optional) tags to save. | ||
*/ | ||
async function putObject(fileName, fileStream, contentType = 'application/json', tags = null) { | ||
if (!fileName) throw new Error('filename is required!'); | ||
if (!fileStream) throw new Error('filestream is required!'); | ||
|
||
const params = { Bucket: bucketId(), Key: fileName, Body: fileStream }; | ||
const options = {}; | ||
|
||
if (contentType) params.ContentType = contentType; | ||
if (tags && typeof tags == 'object') { | ||
options.tags = Object.keys(tags).reduce((L, key) => { | ||
const val = tags[key]; | ||
L.push({ Key: key, Value: `${val}` }); | ||
return L; | ||
}, []); | ||
} | ||
|
||
//! call s3.upload. | ||
// _log(NS, '> params =', params); | ||
return new Promise((resolve, reject) => { | ||
instance().upload(params, options, function(err, data) { | ||
if (err) return reject(err); | ||
resolve(data); | ||
}); | ||
}) | ||
.then(data => { | ||
_log(NS, 'data.key:', (data && data.Key) || '#NOP'); | ||
return data; | ||
}) | ||
.catch(e => { | ||
_err(NS, 'ERR! err=', e); | ||
throw e; | ||
}); | ||
} | ||
|
||
/** | ||
* get a file from S3 Bucket | ||
* | ||
* @param {string} bucketId | ||
* @param {string} fileName | ||
*/ | ||
async function getObject(fileName) { | ||
if (!fileName) throw new Error('filename is required!'); | ||
|
||
const params = { Bucket: bucketId(), Key: fileName }; | ||
|
||
//! call s3.getObject. | ||
// _log(NS, '> params =', params); | ||
return new Promise((resolve, reject) => { | ||
instance().getObject(params, function(err, data) { | ||
if (err) return reject(err); | ||
resolve(data); | ||
}); | ||
}).catch(e => { | ||
_err(NS, 'ERR! err=', e); | ||
throw e; | ||
}); | ||
} | ||
|
||
//! export thiz. | ||
return { hello, putObject, getObject }; | ||
}; |