Example code of File uploading to Amazon S3 using node fastify backend. Fastify uses Fastify multipart for image/ file uploading.
Fastify Route – fileUpload.route.ts
import { FastifyInstance } from "fastify"; import { deleteUploadedFileController, uploadController } from "./fileUpload.controller"; import { $Rref } from "./fileUpload.schema"; import FastifyMultipart from '@fastify/multipart'; export async function fileUploadPublicRoutes(server: FastifyInstance) { server.register(FastifyMultipart); server.post( "/file", { }, uploadController ) server.delete("/file", { schema: { body: $Rref("fileDeleteInputSchema"), } },deleteUploadedFileController) }
Fastify Controller – fileUpload.controller.ts
import { FastifyReply, FastifyRequest } from "fastify"; import { deleteFile, uploadFile } from "../../utils/upload/s3Upload"; import { fileDeleteInput } from "./fileUpload.schema"; export async function uploadController( req: FastifyRequest, reply: FastifyReply ) { const data = await req.file(); const { filename, file} = data; const uniqueFilename = `${Date.now()}-${filename}`; const params = { Bucket: process?.env?.AWS_S3_BUCKET || "img-uploads-bucket", Key: uniqueFilename, Body: file, }; try { const fileUrl = await uploadFile(params); const data = { fileUrl, key: uniqueFilename, }; reply.code(200).send({ message: "File uploaded successfully", data }); console.log("............Upload succesful"); } catch (err) { reply.code(500).send(err); } } export async function deleteUploadedFileController( req: FastifyRequest<{ Body: fileDeleteInput }>, reply: FastifyReply ) { let { fileKey } = req.body; const params = { Bucket: process?.env?.AWS_S3_BUCKET || "img-uploads-bucket", Key: fileKey, }; try { await deleteFile(params); reply.code(200).send({ message: "File delete successfully" }); } catch (err) { reply.code(500).send(err); } }
File Upload – s3Upload.ts
import { S3Client, DeleteObjectCommand } from "@aws-sdk/client-s3"; import { Upload } from "@aws-sdk/lib-storage"; const s3 = new S3Client({ region: process.env.AWS_S3_REGION, credentials: { accessKeyId: process.env.AWS_S3_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_S3_SECRET_KEY, }, }); export async function uploadFile(params) { try { const parallelUploads3 = new Upload({ client: s3, params: params, tags: [ /*...*/ ], // optional tags queueSize: 4, // optional concurrency configuration partSize: 1024 * 1024 * 5, // optional size of each part, in bytes, at least 5MB leavePartsOnError: false, // optional manually handle dropped parts }); parallelUploads3.on("httpUploadProgress", (progress) => { console.log(progress); }); await parallelUploads3.done(); } catch (e) { console.log(e); } } export async function deleteFile(params) { const command = new DeleteObjectCommand(params); try { const response = await s3.send(command); console.log(response); return response; } catch (err) { console.error(err); } }
References: AWS S3 Javascript examples – https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_s3_code_examples.html
Above given is an example of AWS S3 , Javascript V3 example code using fastify multipart. AWS S3 latest image upload tutorial for react apps. AWS s3 file delete fastify example code also give above.