Next.js Discord

Discord Forum

Doubt about dealing with files uploaded by the client

Unanswered
Californian posted this in #help-forum
Open in Discord
CalifornianOP
Hello folks,

In my app, the client can upload some amount of files from his machine (let's say, with a 100 MB upper bound). I have a doubt on how to architecture this. In the client, I represent files with the following data structure:

{file: ArrayBuffer, type: string}


I can think of two options:

1) In the client, I convert the files to a base 64 string representation and I call a server action to transfer those strings to the server side in multiple steps. In the server action, I inject those files in an S3 bucket and later, in my payment webhook, I inject the S3 URLs for the files in my database.

2) In the client, I inject those files in an S3 bucket and later, in my payment webhook, I inject the S3 URLs for the files in my database.

I know (2) is much more efficient in terms of bandwidth but (1) is very safe (no risk to expose my S3 bucket information to the client).

Two questions:

- Is (1) really bad ?

- Is there some way to implement (2) safely and preferably free ?

Thanks

5 Replies

Try to create a presigned url with server action and return it to client
Once your client receives the url, upload the file with it
Google aws s3 presigned url for more information
And you might want to try this package

https://github.com/TimMikeladze/next-upload
CalifornianOP
Thanks Ray, I will try it during the week and I will come back with news
ping me if there are issues with that package