能否使用Node.js的aws-sdk在Amazon S3中解压已上传的Zip文件?
How to Unzip a File in Amazon S3 After Upload Using Node.js & AWS SDK
Great question—you absolutely don’t have to slog through tedious loops to upload each file from your zip manually! There are two practical, scalable ways to handle this using Node.js and the AWS SDK, depending on your workflow:
1. Serverless Approach: Automate Unzipping with AWS Lambda
This is the go-to scalable option, especially if you want the unzip process to run automatically whenever a zip file hits your S3 bucket (perfect for large files or recurring uploads).
Step-by-Step Breakdown:
- Set up an S3 trigger: Configure your S3 bucket to fire a Lambda function whenever a
.zipfile is uploaded. - Build the Lambda function: Use Node.js to fetch the zip, decompress it, and upload the contents back to S3 (preserving folder structure):
Here’s a simplified code snippet for the Lambda handler:
const AWS = require('aws-sdk'); const admZip = require('adm-zip'); const s3 = new AWS.S3(); exports.handler = async (event) => { const sourceBucket = event.Records[0].s3.bucket.name; const zipKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); // Fetch the zip file from S3 const zipFile = await s3.getObject({ Bucket: sourceBucket, Key: zipKey }).promise(); // Unzip the file in memory const zip = new admZip(zipFile.Body); const zipEntries = zip.getEntries(); // Upload each decompressed file/folder to S3 const uploadTasks = zipEntries.map(async (entry) => { if (!entry.isDirectory) { // Preserve the original folder structure in the target path const targetKey = `unzipped/${entry.entryName}`; await s3.putObject({ Bucket: sourceBucket, Key: targetKey, Body: entry.getData(), ContentType: entry.header?.extra?.contentType || 'application/octet-stream' }).promise(); } }); await Promise.all(uploadTasks); return { status: 'success', message: 'Files unzipped and uploaded to S3' }; };
Quick Notes:
- Permissions: Make sure your Lambda execution role has S3 read access to the source bucket and write access to the target location.
- Large Files: For zips over a few hundred MB, swap
adm-zipwithunzipperto stream decompression (avoids loading the entire file into memory).
2. Application-Side Unzipping
If you prefer handling the process directly in your Node.js app (no Lambda needed), you can:
- Upload the zip to S3 first.
- Download it back to your server.
- Decompress it locally/in memory.
- Upload each file to S3, keeping the original folder structure intact.
Here’s a quick example:
const AWS = require('aws-sdk'); const admZip = require('adm-zip'); const s3 = new AWS.S3({ region: 'your-region' }); async function unzipAndUploadFromS3(sourceBucket, sourceZipKey, targetBucket) { // Download the zip from S3 const zipResponse = await s3.getObject({ Bucket: sourceBucket, Key: sourceZipKey }).promise(); // Unzip the file const zip = new admZip(zipResponse.Body); const entries = zip.getEntries(); // Upload each decompressed file for (const entry of entries) { if (!entry.isDirectory) { const targetKey = `unzipped/${entry.entryName}`; await s3.putObject({ Bucket: targetBucket, Key: targetKey, Body: entry.getData() }).promise(); console.log(`Uploaded ${targetKey}`); } } } // Run the function unzipAndUploadFromS3('your-source-bucket', 'uploads/archive.zip', 'your-target-bucket') .then(() => console.log('Unzip and upload complete!')) .catch(err => console.error('Process failed:', err));
Key Tips:
- Memory Management: For large zips, use streaming decompression (with
unzipper) instead of loading the entire file into memory. - Folder Structure: Both methods preserve the zip’s original directory hierarchy, so you don’t have to manually rebuild paths.
- Credentials: Ensure your Node.js app has the right S3 permissions (via IAM roles or access keys) to read the source zip and write decompressed files.
内容的提问来源于stack exchange,提问作者Mahmoud Niypoo
相关产品推荐
相关产品推荐

