FoalTS / foal

Full-featured Node.js framework 🚀
https://foalts.org/
MIT License
1.9k stars 142 forks source link

Generate upload/download pre-signed urls with the Disk util #1160

Open k0rn4l opened 2 years ago

k0rn4l commented 2 years ago

We don't always want to server to handle file uploads, and it's easy enough to use the AWS SDK, but the Disk util is just too convenient. Would it be possible to add this feature to it?

Something like:

 const { downloadUrl } = await this.disk.createPresignedDownloadUrl('docs/xxx.pdf', 'buffer', {
              expiresIn: 3600
          });
 const { uploadUrl } = await this.disk.createPresignedUploadUrl('docs', content, {
              expiresIn: 3600
          });

Thought this does present a problem, to how to handle local file upload/downloads in this case, I don't have good idea for that one.

kingdun3284 commented 2 years ago

You can write your own disk that extends the original disk.

For example: https://github.com/codeperate/foal-s3-disk-v3/blob/main/src/s3-disk.service.ts

This is my own implementation that change the s3 disk service to use aws-sdk v3.

LoicPoullain commented 2 years ago

Hi @k0rn4l 👋

Thank you for your suggestion.

I'm not very familiar with presigned upload/download URLs. Would you have some examples in mind that would show how it works in practice?

k0rn4l commented 1 year ago

Hi @LoicPoullain !

So the idea here is to skip the processing of the uploaded file for the backend, and instead upload it directly to a storage bucket from the frontend ( or other service ). This can be done securely by getting a signed upload url via the AWS SDK from your backend, and send that URL to the frontend. The admin can then use that URL to upload files to the pre selected bucket/folder. The URL expires in the set amount of time.

Same for the download URL. A temporary download link is created, and you can download the contents from there until it expires.