Skip to content

Instantly share code, notes, and snippets.

@alouini333
Last active December 14, 2021 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alouini333/3c99725479d345430679c05ec538841f to your computer and use it in GitHub Desktop.
Save alouini333/3c99725479d345430679c05ec538841f to your computer and use it in GitHub Desktop.
const unzipper = require('unzipper');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const { TARGET_BUCKET } = process.env;
exports.handler = async (event) => {
try {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
console.log(`Processing zip file: ${key} in bucket:${bucket} `);
const directory = await unzipper.Open.s3(s3, { Bucket: bucket, Key: key });
const zip = s3.getObject({ Bucket: bucket, Key: key })
.createReadStream()
.pipe(unzipper.Parse({forceStream: true}));
const { TARGET_BUCKET } = process.env;
for await (const entry of zip) {
const Bucket = TARGET_BUCKET;
const Key = `${path}/${entry.path}`;
await s3.upload({Bucket, Key, Body: entry}).promise()
}
} catch (e) {
return {
statusCode: 400,
body: e.message
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment