Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const AWS = require("aws-sdk");
const stream = require("stream");
const yauzl = require("yauzl");
const mime = require('mime-types');

const { v4: uuidv4 } = require("uuid");

const uploadStream = ({ Bucket, Key }) => {
const uploadStream = ({ Bucket, Key, ContentType }) => {
const s3 = new AWS.S3();
const pass = new stream.PassThrough();
return {
writeStream: pass,
promise: s3.upload({ Bucket, Key, Body: pass }).promise(),
promise: s3.upload({ Bucket, Key, Body: pass, ContentType}).promise(),
};
};

Expand All @@ -27,15 +29,16 @@ const extractZip = (Bucket, buffer) => {
zipfile.openReadStream(entry, function (err, readStream) {
if (err) reject(err);
const fileNames = entry.fileName.split(".");

//adding the mime type for the created file.
const { writeStream, promise } = uploadStream({
Bucket,
Key: `${fileNames[0]}.${uuidv4()}.${
fileNames[fileNames.length - 1]
}`,
Key: entry.fileName,
ContentType: mime.lookup(entry.fileName)
});

readStream.pipe(writeStream);
promise.then(() => {
console.log(entry.fileName + " Uploaded successfully!");
zipfile.readEntry();
});
});
Expand All @@ -61,6 +64,19 @@ exports.handler = async (event) => {
const object = await s3.getObject(params).promise();
const result = await extractZip(Bucket, object.Body);

console.log("Zip file successfully extracted.");

//deleting the original zip file.
await s3.headObject(params).promise()
console.log("File Found in S3")
try {
await s3.deleteObject(params).promise()
console.log("file deleted Successfully")
}
catch (err) {
console.log("ERROR in file Deleting : " + JSON.stringify(err))
}

return {
status: result && 200,
response: result && "OK",
Expand All @@ -71,4 +87,4 @@ exports.handler = async (event) => {
console.log(message);
throw new Error(message);
}
};
};
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"aws-sdk": "^2.1015.0",
"mime-types": "^2.1.35",
"uuid": "^8.3.2",
"yauzl": "^2.10.0"
},
Expand Down