skimah
skimah

Reputation: 111

Remove files from Public folder from client with Next.js using routes api

I have an application where the user generates QR codes. These generated qr codes are stored as png in the public folder so that he can access them and download them. The user can delete the QR codes as he wishes and I dont want the QR codes to stay in the public folder if it wont use them. Is there anyway to access the public folder from the routes API and have some function delete a specific QR code's png? Thanks!

Upvotes: 0

Views: 2313

Answers (1)

giraffesyo
giraffesyo

Reputation: 5342

Resources in the public folder are statically generated and shouldn't be modified during runtime. In development, it works because the dev server is doing some magic for you, but it's not a scalable solution. Assuming you could get this to work, you will run into issues when you want to scale out your service.

Consider that you run multiple instances of your Next.js web application. Which public folder would the file go into? What if you have your application versioned through source control and you deploy it to a remote service like Vercel or AWS using CI/CD. What will happen to the images in the public folder when the new version is pushed?

Instead of trying to upload resources to the public folder during runtime, I recommend using a SaaS solution to host the QR codes. Good options are AWS S3 or Cloudinary. You can upload the image temporarily to the API route and then use an API to upload the image to the service you choose.

Alternatively, you can use something like AWS s3 presigned URLs to make uploading images even easier. Simply have your API route return a presigned URL to the client and have the client upload directly to Amazon's servers without having your application serve as a middleman.

Upvotes: 1

Related Questions