Skip to content

Commit

Permalink
feat: upload json to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-lemon committed Aug 6, 2019
1 parent 7bf421d commit c8dec56
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/api/hello-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ module.exports = (_$, name) => {
next = do_get_test_sns_err;
} else if (ID !== '!' && CMD === 'test-encrypt') {
next = do_get_test_encrypt;
} else if (ID !== '!' && CMD === 'test-s3-put') {
next = do_get_test_s3_put;
}
break;
case 'PUT':
Expand Down Expand Up @@ -117,6 +119,11 @@ module.exports = (_$, name) => {
return _$.sns;
};

const $s3s = function() {
if (!_$.s3s) throw new Error('$s3s(s3s-service) is required!');
return _$.s3s;
};

//! shared memory.
// WARN! - `serverless offline`는 상태를 유지하지 않으므로, NODES값들이 실행때마다 리셋이될 수 있음.
const NODES = [
Expand Down Expand Up @@ -441,6 +448,20 @@ module.exports = (_$, name) => {
});
}

/**
* Test S3 PutObject.
*
* ```sh
* $ http ':8888/hello/0/test-s3-put'
*/
function do_get_test_s3_put(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_s3_put(${ID})....`);
const message = 'hello lemon';
const data = { message };
const json = JSON.stringify(data);
return $s3s().putObject('test.json', json, 'application/json');
}

const a = 1;
// eslint-disable-next-line no-console
console.log('a =', a);
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ function initialize($export) {
//! load basic core services......
const $kms = require('./service/kms-service')($lemon);
const $sns = require('./service/sns-service')($lemon);
const $s3s = require('./service/s3s-service')($lemon);
$lemon('kms', $kms);
$lemon('sns', $sns);
$lemon('s3s', $s3s);

//! load api functions............
const hello = require('./api/hello-api')($lemon);
Expand Down
130 changes: 130 additions & 0 deletions src/service/s3s-service.js
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 };
};

0 comments on commit c8dec56

Please sign in to comment.