Timo Jokinen
Timo Jokinen

Reputation: 726

Upload to AWS S3 Best Practice

I'm creating a node backend application and I have an entity which can have files assigned.

I have the following options:

  1. Make a request and upload the files as soon as the user selects them in the frontend form and assign them to the entity when the user makes the request to create / update it
  2. Upload the files in the same request which creates / updates the entity

I was wondering if there is a best practice for this scenario. I can't really decide whats better.

Upvotes: 0

Views: 2002

Answers (1)

clay
clay

Reputation: 6017

This is one of those "depends" answers, and it depends how you are doing uploads and if you plan to clean up your S3 buckets.

I'd suggest creating the entity first (option #2), because than you can store what S3 files are with that entity. If you tried option #1, you might have untracked files (or some kind of staging area), which could require cleanup at some point in the future. (If you files are small, it may never matter, and you just eat that $0.03/GB fee each month : )

I've been seeing on some web sites that look like option #1, where files are included in my form/document as I'm "editing". Pasting an image from my buffer is particularly sweet, and sometimes I see a placeholder of text while it is uploaded, showing the picture when complete. Now I think these "documents" are actually saved on their servers in some kind of draft status, so it might be your option #2 anyway. You could do the same that creates a draft entity, and finalizes it later on (and then have a way to clean out drafts and their attachments at some point).

Also, depending on bucket privacy you need to achieve, have a look at AWS Cognito to upload directly from the browser. You could save your server bandwidth, and reduce your request time, by not using your server as as pass-through.

Upvotes: 2

Related Questions