Skip to content

Commit ad0b475

Browse files
committed
initial commit
0 parents  commit ad0b475

9 files changed

+969
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
output.yml
3+
node_modules
4+
.vscode
5+
.idea

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# S3 Bucket -> Lambda (Convert CSV to Excel XLSX) -> S3 Bucket
3+
4+
## Description
5+
6+
This is a serverless component that takes uploaded CSV files from one S3 Bucket, converts them to Microsoft Excel XLSX and uploads to another S3 Bucket. It contains:
7+
8+
- an Input S3 Bucket that accepts files.
9+
10+
- a Lambda that takes the CSV file from the Input S3 bucket, converts it to a Microsoft Excel XLSX and uploads it to the Output one
11+
12+
- an Output S3 Bucket that receives files.
13+
14+
## Deployment Parameters
15+
16+
This component has one CloudFormation deployment parameter:
17+
18+
- `ConversionTimeout`, an optional parameter, represents the timeout of the Conversion Lambda function. By default its 60 seconds.
19+
20+
## Latest Release - 1.0.0
21+
22+
Initial Release.
23+
24+
## Roadmap - Upcoming changes
25+
26+
Here are the upcoming changes that I'll add to this serverless component:
27+
28+
- ESLint
29+
- Tests

convert.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const ExcelJS = require('exceljs');
3+
4+
function readCSV(filepath) {
5+
const workbook = new ExcelJS.Workbook();
6+
return workbook.csv.readFile(filepath)
7+
.then(() => workbook);
8+
}
9+
10+
function writeXLSX(filepath, workbook) {
11+
return workbook.xlsx.writeFile(filepath);
12+
}
13+
14+
module.exports = function convert(inFile, outFile) {
15+
return readCSV(inFile)
16+
.then(workbook => writeXLSX(outFile, workbook));
17+
};
18+

index.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
const s3Util = require('./s3-util'),
3+
path = require('path'),
4+
convert = require('./convert'),
5+
os = require('os'),
6+
EXTENSION = process.env.EXTENSION,
7+
OUTPUT_BUCKET = process.env.RESULTS_BUCKET_NAME,
8+
MIME_TYPE = process.env.MIME_TYPE;
9+
10+
exports.handler = function (eventObject, context) {
11+
const eventRecord = eventObject.Records && eventObject.Records[0],
12+
inputBucket = eventRecord.s3.bucket.name,
13+
key = eventRecord.s3.object.key,
14+
id = context.awsRequestId,
15+
resultKey = key.replace(/\.[^.]+$/, EXTENSION),
16+
tempPath = path.join(os.tmpdir(), id),
17+
convertedPath = path.join(os.tmpdir(), 'converted-' + id + EXTENSION);
18+
19+
console.log('converting', inputBucket, key, 'using', tempPath);
20+
return s3Util.downloadFileFromS3(inputBucket, key, tempPath)
21+
.then(() => convert(tempPath, convertedPath))
22+
.then(() => s3Util.uploadFileToS3(OUTPUT_BUCKET, resultKey, convertedPath, MIME_TYPE));
23+
};

output.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Description: A serverless component for CSV to XLSX conversion
3+
Outputs:
4+
InputS3Bucket:
5+
Description: Input S3 bucket
6+
Value:
7+
Ref: InputS3Bucket
8+
OutputS3Bucket:
9+
Description: Output S3 bucket
10+
Value:
11+
Ref: OutputS3Bucket
12+
Parameters:
13+
ConversionTimeout:
14+
Default: 60
15+
Type: Number
16+
Resources:
17+
ConvertFileFunction:
18+
Properties:
19+
CodeUri: s3://sar-components-bucket/1a4741b1dd37f9db2268dc7f1a32a678
20+
Environment:
21+
Variables:
22+
EXTENSION: .xlsx
23+
MIME_TYPE: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
24+
RESULTS_BUCKET_NAME:
25+
Ref: OutputS3Bucket
26+
Events:
27+
FileUpload:
28+
Properties:
29+
Bucket:
30+
Ref: InputS3Bucket
31+
Events: s3:ObjectCreated:*
32+
Type: S3
33+
Handler: index.handler
34+
MemorySize: 512
35+
Policies:
36+
- S3CrudPolicy:
37+
BucketName:
38+
Fn::Sub: ${AWS::StackName}-*
39+
Runtime: nodejs8.10
40+
Timeout:
41+
Ref: ConversionTimeout
42+
Type: AWS::Serverless::Function
43+
InputS3Bucket:
44+
Type: AWS::S3::Bucket
45+
OutputS3Bucket:
46+
Type: AWS::S3::Bucket
47+
Transform: AWS::Serverless-2016-10-31

0 commit comments

Comments
 (0)