Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const os = require('os');
- const fs = require('fs');
- const path = require('path');
- const readline = require('readline');
- const aws = require('aws-sdk');
- const s3 = new aws.S3();
- async function downloadFileFromS3 (bucket, fileKey, filePath) {
- console.log('downloading', bucket, fileKey, filePath);
- return new Promise(function (resolve, reject) {
- const file = fs.createWriteStream(filePath),
- stream = s3.getObject({
- Bucket: bucket,
- Key: fileKey
- }).createReadStream();
- stream.on('error', reject);
- file.on('error', reject);
- file.on('finish', function () {
- console.log('downloaded', bucket, fileKey);
- resolve(filePath);
- });
- stream.pipe(file);
- });
- };
- async function processFile(filePath) {
- console.log(`Processing ${filePath}.`);
- return new Promise(function (resolve, reject) {
- const outputPath = `${filePath}.out`;
- const readFile = readline.createInterface({
- input: fs.createReadStream(filePath),
- output: fs.createWriteStream(outputPath),
- terminal: false
- });
- readFile
- .on('line', transform)
- .on('close', function() {
- console.log(`Created "${this.output.path}"`);
- resolve(outputPath);
- });
- function transform(line) {
- this.output.write(line.toUpperCase() + '\n');
- }
- });
- }
- async function uploadFileToS3 (bucket, fileKey, filePath, contentType) {
- console.log('uploading', bucket, fileKey, filePath);
- return s3.upload({
- Bucket: bucket,
- Key: fileKey,
- Body: fs.createReadStream(filePath),
- ACL: 'private',
- ContentType: contentType
- }).promise();
- };
- exports.handler = async function (eventObject, context) {
- const OUTPUT_BUCKET = 'demo-s3-output';
- const eventRecord = eventObject.Records && eventObject.Records[0];
- const inputBucket = eventRecord.s3.bucket.name;
- const key = eventRecord.s3.object.key;
- const workdir = os.tmpdir();
- const inputFile = path.join(workdir, key);
- console.log(`Transforming ${key} using ${inputFile}`);
- await downloadFileFromS3(inputBucket, key, inputFile);
- const newFile = await processFile(inputFile);
- await uploadFileToS3(OUTPUT_BUCKET, key, newFile);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement