Next.js Discord

Discord Forum

Private assets in nextjs, such as PDF

Answered
German Shorthaired Pointer posted this in #help-forum
Open in Discord
German Shorthaired PointerOP
Where do I put private assets that will be served only to auth users through api routes?
Answered by joulev
But if you don’t need to write and you self-host then you can simply put the file in any folders outside public and it will be private yes
View full answer

9 Replies

@German Shorthaired Pointer Where do I put private assets that will be served only to auth users through api routes?
An object storage service, such as aws s3, google cloud storage, cloudflare r2, etc
@German Shorthaired Pointer Where do I put private assets that will be served only to auth users through api routes?
I prefer using s3 and enable auth. Like that you need a token to access the asset and only your server knows the token
German Shorthaired PointerOP
Is it possible with Next.js only? If I have the pdf file in a folder that's not referenced anywhere apart from the api route, will it be public despite that?
@German Shorthaired Pointer Is it possible with Next.js only? If I have the pdf file in a folder that's not referenced anywhere apart from the api route, will it be public despite that?
It won’t be public, no. But where are you hosting? Using fs to read the file is only possible in some circumstances on Vercel, writing to that file on-the-fly is completely impossible in all hosting methods, and storing the file in the public folder is out of the question
German Shorthaired PointerOP
I'm hosting on azure. I don't need the option to write to it but I might consider an object storage services, as you have suggested
feels like the most optimal way
Iirc azure does offer its own native object storage service so I would use that
But if you don’t need to write and you self-host then you can simply put the file in any folders outside public and it will be private yes
Answer
German Shorthaired PointerOP
Appreciate the help!