Last active
December 14, 2021 09:18
-
-
Save alouini333/3c99725479d345430679c05ec538841f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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