Louis
Louis

Reputation: 65

How should I store files in NodeJS

Upvotes: 2

Views: 9064

Answers (2)

shrotavre
shrotavre

Reputation: 98

Its common for companies to opt for 3rd party's or separate storage service that optimized for file storage. Some comes with free-trial-ish plan like aws-s3, Google's gcs, azure, and bunch others.

But if it really comes to a file storage in our own service, it's good enough to put all the files in the single same folder (flat file directory) and do the file categorization by attaching the file to the corresponding models. Putting something like this in the data: { "username": "arege", "name": "Arle Greg", "profile_picture_file":"timestamp_somehash.png"}

For the second question, if your service need to record dates for all the uploaded file types in general, then its good to have that kind of model. But if its only for a little part of the file types in the service, it can just be written in the corresponding model along with the file link.

Upvotes: 1

paulzmuda
paulzmuda

Reputation: 181

  • The static public folder should be your only exposed folder, so in a sense you have it right.
  • No, I would use an external service like AWS S3 to store user assets. The folder pattern which you decide on entirely depends on what assets you plan to store for your users. Which way makes it easy for you to debug, maintain sanity, and the ability to move to other servers. Can you split your users between servers? I would draw out a tree of filetypes you expect and think about what pattern is the most flexible in your application. I would lean towards types since some files are handled differently than others. You also don't want hundreds of nearly empty folders from inactive users.
  • Yes. Also include file path on the server to be re-used later. I would also think about naming conventions of the files as a backup identifier. Maybe something like userid_date_avatar.jpg.

{ id: 12345, path: "s3://app_name/users/assets/images/5543_01_31_2019_10_05_33_avatar.jpg", uploaded: "01-31-2019_10:05:33" }

Upvotes: 2

Related Questions