S3 is a scalable storage infrastructure from Amazon Web Services (AWS), and provides mechanisms to store and retrieve objects(files) through web services.
The article includes steps to:
i). Upload file(s) to S3 bucket with api using node.js and koa.
ii). Downloading the uploaded files from S3, and archiving it.
Prerequisites:
You need to have following modules installed:
npm i koanpm i koa-bodynpm i koa-routernpm i aws-sdknpm i archiver
Step 1: Setup koa and define API routes to upload and download files.
File upload is done through “multipart/form-data”, hence remember to set the {multipart:true} option.
Step 2: Create “uploadFile” functionality to upload the files.
The above “uploadFile()” can take a single or multiple files for upload at once.
Here we have used the “aws-sdk” module which is the official AWS SDK for JavaScript.
In the above function “ctx” is the context of koa which encapsulates node’s request and response objects.
The “s3.upload()” functionality returns a callback which contains details of files after upload.
Eg.:
{ ETag: ‘nsb0uiy01234567890123e01fg0123456h’, Location: ‘https://<YOUR_BUCKET>/files/temp.JPG’, key: ‘files/file1.JPG’, Key: ‘files/file1.JPG’, Bucket: <YOUR_BUCKET>’}
Here the “Location” attribute contains the link to the uploaded file.
Step 3: Create “downloadArchive” functionality to download the files and archive it into a zip file.
Here we have used the “archiver” module, which helps to create archive files like “zip” files.
The s3.getObject() returns the details of the updated file, to which we can chain the createReadStream() function of node.js. The “append()” function of archiver takes this stream and the name of the uploaded file to be included into the archive. The “finalize()” will create our zip file with the name “myfiles.zip”.
With “ctx.response.set” we set “Content-disposition” to indicate the client to download the file, and provide the name for the attachment.
And finally “ctx.body” will send the file to the frontend client.
With “fs.unlinkSync()” we delete the archive that was created, as a part of the cleanup process.
Thanks for reading.
Please check out my other articles.