Skip to content

Commit c06c9b2

Browse files
author
gaupoit
authored
Merge pull request #11 from ymese/develop
Develop
2 parents c09507f + 70fd5ff commit c06c9b2

File tree

9 files changed

+266
-59
lines changed

9 files changed

+266
-59
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
test.js
3-
config.json
3+
config.json
4+
.vscode

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
1-
# nodejs-aws-utils
2-
Utilities function to work with AWS
1+
# Nodejs-aws-utils
2+
> Utilities function to work with AWS
3+
4+
Nodejs-aws-utils makes aws-sdk easier by provide a libary for **s3 bucket**, **dynamoDB**, **CloudFront** and support **promise**
5+
6+
## Installation
7+
The easiest way to use nodejs-aws-utils is to install it from npm
8+
```sh
9+
npm i nodejs-aws-utils --save
10+
```
11+
12+
## Config
13+
Config default :
14+
```js
15+
const awsUtils = require('nodejs-aws-utils');
16+
awsUtils.config('ACCESS_KEY_ID', 'SECRET_ACCESS_KEY', 'REGION');
17+
```
18+
Config by file:
19+
```js
20+
const awsUtils = require('nodejs-aws-utils');
21+
awsUtils.configByFile('path');
22+
```
23+
## DynamoDB
24+
Import csv file to dynamoDB :
25+
```js
26+
const awsUtils = require('./index');
27+
awsUtils.dynamoDB.importCSV('table_name', './dat.csv')
28+
.then((data) => {
29+
console.log('Success', data);
30+
})
31+
.catch(err => console.log(err.stack));
32+
```
33+
Export csv file from dynamoDB :
34+
```js
35+
const awsUtils = require('./index');
36+
awsUtils.dynamoDB.exportCSV({ TableName: 'table_name' }, './dat.csv')
37+
.then((status) => {
38+
if (status === true) {
39+
console.log('Success');
40+
} else {
41+
console.log('Fail');
42+
}
43+
});
44+
45+
```
46+
## S3 buckets
47+
List buckets :
48+
```js
49+
awsUtils.bucket.list()
50+
.then(data => console.log(data))
51+
.catch(err => console.log(err));
52+
```
53+
Create s3 bucket :
54+
```js
55+
awsUtils.bucket.create('bucket_name')
56+
.then(data => console.log(data))
57+
.catch(err => console.log(err));
58+
```
59+
Upload file to s3 bucket :
60+
```js
61+
awsUtils.bucket.uploadFile('bucket_name', './pic.png')
62+
.then(data => console.log(data))
63+
.catch(err => console.log(err));
64+
```
65+
## CloudFront
66+
Create distribution default :
67+
```js
68+
awsUtils.cloudFront.createDistributionDefault({
69+
id: 'example_name_id',
70+
domainName: 'examplename.s3.amazonaws.com',
71+
}).then(data => console.log(data))
72+
.catch(err => console.log(err));
73+
```
74+
Create distribution with [parameters]((https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFront.html#createDistribution-property)) :
75+
```js
76+
awsUtils.cloudFront.createDistribution(parameters)
77+
.then(data => console.log(data))
78+
.catch(err => console.log(err));
79+
```
80+
Get distribution config default:
81+
```js
82+
awsUtils.cloudFront.getDistributionConfig();
83+
```
84+
Set distribution config:
85+
```js
86+
awsUtils.cloudFront.setDistributionConfig();
87+
```
88+
89+
## Release history
90+
91+
## Meta
92+
Ymese Team - Ymese.com
93+
Distributed under the APACHE license. See LICENSE for more information.

bucketS3Util.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const aws = require('aws-sdk');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
module.exports = {
6+
list() {
7+
const s3 = new aws.S3();
8+
return s3.listBuckets().promise();
9+
},
10+
create(bucketName) {
11+
const s3 = new aws.S3();
12+
const bucketParams = {
13+
Bucket: bucketName,
14+
};
15+
return s3.createBucket(bucketParams).promise();
16+
},
17+
uploadFile(bucketName, fileName) {
18+
const s3 = new aws.S3();
19+
const uploadParams = { Bucket: bucketName, Key: '', Body: '' };
20+
const fileStream = fs.createReadStream(fileName);
21+
fileStream.on('error', (err) => {
22+
console.log('File Error', err);
23+
});
24+
uploadParams.Body = fileStream;
25+
uploadParams.Key = path.basename(fileName);
26+
return s3.upload(uploadParams).promise();
27+
},
28+
};

cloudFrontUtil.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const aws = require('aws-sdk');
2+
3+
let distributionConfig = {
4+
DistributionConfig: {
5+
Comment: '',
6+
CacheBehaviors: {
7+
Quantity: 0,
8+
},
9+
Logging: {
10+
Bucket: '',
11+
Prefix: '',
12+
Enabled: false,
13+
IncludeCookies: false,
14+
},
15+
Origins: {
16+
Items: [
17+
{
18+
OriginPath: '',
19+
CustomOriginConfig: {
20+
OriginProtocolPolicy: 'http-only',
21+
HTTPPort: 80,
22+
HTTPSPort: 443,
23+
},
24+
Id: '',
25+
DomainName: '',
26+
},
27+
],
28+
Quantity: 1,
29+
},
30+
PriceClass: 'PriceClass_All',
31+
Enabled: true,
32+
DefaultCacheBehavior: {
33+
TrustedSigners: {
34+
Enabled: false,
35+
Quantity: 0,
36+
},
37+
TargetOriginId: '',
38+
ViewerProtocolPolicy: 'allow-all',
39+
ForwardedValues: {
40+
Headers: {
41+
Quantity: 0,
42+
},
43+
Cookies: {
44+
Forward: 'none',
45+
},
46+
QueryString: false,
47+
},
48+
SmoothStreaming: false,
49+
AllowedMethods: {
50+
Items: ['GET', 'HEAD'],
51+
CachedMethods: {
52+
Items: ['GET', 'HEAD'],
53+
Quantity: 2,
54+
},
55+
Quantity: 2,
56+
},
57+
MinTTL: 0,
58+
},
59+
CallerReference: '',
60+
CustomErrorResponses: {
61+
Quantity: 0,
62+
},
63+
Restrictions: {
64+
GeoRestriction: {
65+
RestrictionType: 'none',
66+
Quantity: 0,
67+
},
68+
},
69+
Aliases: {
70+
Quantity: 0,
71+
},
72+
},
73+
};
74+
75+
76+
module.exports = {
77+
createDistributionDefault({ id, domainName }) {
78+
const cloudFront = new aws.CloudFront({ apiVersion: '2018-06-18' });
79+
distributionConfig.DistributionConfig.Origins.Items[0].Id = id;
80+
distributionConfig.DistributionConfig.Origins.Items[0].DomainName = domainName;
81+
distributionConfig.DistributionConfig.DefaultCacheBehavior.TargetOriginId = id;
82+
distributionConfig.DistributionConfig.CallerReference = Date.now().toString();
83+
return cloudFront.createDistribution(distributionConfig).promise();
84+
},
85+
createDistribution(params) {
86+
const cloudFront = new aws.CloudFront({ apiVersion: '2018-06-18' });
87+
return cloudFront.createDistribution(params).promise();
88+
},
89+
getDistributionConfig() {
90+
return distributionConfig;
91+
},
92+
setDistributionConfig(params) {
93+
distributionConfig = params;
94+
},
95+
};

configAWS.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const aws = require('aws-sdk');
2+
3+
module.exports = {
4+
configByFile(pathConfig) {
5+
aws.config.loadFromPath(pathConfig);
6+
},
7+
config(accessKeyId, secretAccessKey, region) {
8+
aws.config.update({ accessKeyId, secretAccessKey, region });
9+
},
10+
};

dynamoDBCSV.js

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const aws = require('aws-sdk');
44
const path = require('path');
55
const csvToJson = require('csvtojson');
66

7-
let dynamoDB;
8-
97
function generateDataDynamoDB(tableName, data) {
108
const params = {
119
RequestItems: {
@@ -24,15 +22,9 @@ function generateDataDynamoDB(tableName, data) {
2422
}
2523

2624
function bulkData(tableName, data) {
25+
const dynamoDB = new aws.DynamoDB();
2726
const params = generateDataDynamoDB(tableName, data);
28-
console.log(params);
29-
dynamoDB.batchWriteItem(params, (err) => {
30-
if (err) {
31-
throw err;
32-
} else {
33-
console.log('Imported');
34-
}
35-
});
27+
return dynamoDB.batchWriteItem(params).promise();
3628
}
3729

3830
function createFile(locaFile, csv) {
@@ -44,49 +36,38 @@ function createFile(locaFile, csv) {
4436
fs.mkdirSync(path.dirname(locationFile));
4537
fs.writeFileSync(locationFile, csv);
4638
}
39+
return Promise.resolve(true);
4740
} catch (err) {
48-
throw err;
41+
return Promise.reject(err);
4942
}
5043
}
5144

52-
module.exports = {
53-
configByFile(pathConfig) {
54-
aws.config.loadFromPath(pathConfig);
55-
dynamoDB = new aws.DynamoDB();
56-
},
57-
config(accessKeyId, secretAccessKey, region) {
58-
aws.config.update({ accessKeyId, secretAccessKey, region });
59-
dynamoDB = new aws.DynamoDB();
60-
},
61-
export(tableName, locationFile) {
62-
const params = {
63-
TableName: tableName,
64-
};
65-
dynamoDB.scan(params, (error, data) => {
66-
if (error) {
67-
throw error;
68-
} else {
69-
const { Items: items } = data;
70-
try {
71-
const records = [];
72-
items.forEach((element) => {
73-
records.push(aws.DynamoDB.Converter.unmarshall(element));
74-
});
75-
const fields = Object.keys(records[0]);
76-
const parser = new Json2csvParser({ fields });
77-
const csv = parser.parse(records);
78-
createFile(locationFile, csv);
79-
} catch (err) {
80-
throw err;
81-
}
82-
}
45+
function csvFileToJson(data) {
46+
const { Items: items } = data;
47+
try {
48+
const records = [];
49+
items.forEach((element) => {
50+
records.push(aws.DynamoDB.Converter.unmarshall(element));
8351
});
52+
const fields = Object.keys(records[0]);
53+
const parser = new Json2csvParser({ fields });
54+
const csv = parser.parse(records);
55+
return Promise.resolve(csv);
56+
} catch (err) {
57+
return Promise.reject(err);
58+
}
59+
}
60+
61+
module.exports = {
62+
exportCSV(params, locationFile) {
63+
const dynamoDB = new aws.DynamoDB();
64+
return dynamoDB.scan(params).promise()
65+
.then(response => csvFileToJson(response))
66+
.then(csv => createFile(locationFile, csv));
8467
},
85-
import(tableName, csvFilePath) {
86-
csvToJson()
68+
importCSV(tableName, csvFilePath) {
69+
return csvToJson()
8770
.fromFile(csvFilePath)
88-
.then((jsonObj) => {
89-
bulkData(tableName, jsonObj);
90-
});
71+
.then(jsonObj => bulkData(tableName, jsonObj));
9172
},
9273
};

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
const dynamoDBCSV = require('./dynamoDBCSV');
1+
const dynamoDB = require('./dynamoDBCSV');
2+
const config = require('./configAWS');
3+
const bucket = require('./bucketS3Util');
4+
const cloudFront = require('./cloudFrontUtil');
25

36
module.exports = {
4-
config: dynamoDBCSV.config,
5-
configByFile: dynamoDBCSV.configByFile,
6-
import: dynamoDBCSV.import,
7-
export: dynamoDBCSV.export,
7+
config: config.config,
8+
configByFile: config.configByFile,
9+
dynamoDB,
10+
bucket,
11+
cloudFront,
812
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)