Saving Millions of Images Series [Part Two]
Tags: aws, javascript, codequality
Published: 2022-01-12 15:26:22

**Saving assets**

Ok, I agree with you that one of the easiest way is to work with S3 bucket. But there is more to talk about.


Lets’s talk about how to organize the storing of images. We can have a long discussion about how to store "paths to images."

A guy from a StackOverflow posted this: https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay/3751#3751

What about image names? can be something like this:

I would store the image hash in the database along with a 
`DATE / DATETIME / TIMESTAMP` field indicating when the image was uploaded/processed and then place the image in a structure like this


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tknkt7jk8vsxyuq7lnh.png)

 Or 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8alfvbja1c4ryk142mf.png)

   
Ok, this method is more descriptive; I get it. But this structure is significant to host hundreds of thousands (depending on your file system boundaries) of images per day for several thousand years, this is how WordPress does it, and I think they got it right.

Duplicated images could be easily queried on the database, and you'd have to create symlinks.

I dont like to operate with user IDs unless you don't have that info available in your database, because of:
- Disclosure of usernames in the URL
- Usernames are volatile (you may be able to rename folders, but again...)
- A user can hypothetically upload a large number of images


Don't forget about CDN! This scheme might work well with CDN. At least I don't see anything bad

Read those >= 10 articles about uploading images or check out these simple examples



* [How to Upload and Display Images with JavaScript](https://medium.com/@iamcodefoxx/how-to-upload-and-preview-an-image-with-javascript-749b92711b91)
* [Using files from web applications - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications)
* [How to Preview image before uploading in Javascript - DEV Community](https://dev.to/shantanu_jana/how-to-preview-image-before-uploading-in-javascript-1f6g)
* [[Solved] Javascript how to upload image file from url using FileReader API? - Code Redirect](https://coderedirect.com/questions/372973/how-to-upload-image-file-from-url-using-filereader-api)
* [https://javascript.plainenglish.io/upload-images-in-your-node-app-e05d0423fd4a?gi=75cd4825bb8e](https://javascript.plainenglish.io/upload-images-in-your-node-app-e05d0423fd4a?gi=75cd4825bb8e)
* [JavaScript SDK image and video upload | Cloudinary](https://cloudinary.com/documentation/javascript_image_and_video_upload)
* [Saving images in a database or in the project's folder? - DEV Community](https://dev.to/alex002i/saving-images-in-a-database-or-in-the-project-s-folder-3b3g)
* [Client-side image upload in JavaScript without managing storage](https://imagekit.io/blog/client-side-file-upload/)
* [How to make a simple image upload using Javascript/HTML - Stack Overflow](https://stackoverflow.com/questions/22087076/how-to-make-a-simple-image-upload-using-javascript-html)
* [How to Build an Angular Image Feed | Hacker Noon](https://hackernoon.com/how-to-build-an-angular-js-image-feed-ea6193fa0b75?source=rss)
* [Three Ways of Storing and Accessing Lots of Images in Python](https://realpython.com/storing-images-in-python/)
* [https://vemto.app/blog/how-to-create-an-image-upload-viewer-with-alpinejs](https://vemto.app/blog/how-to-create-an-image-upload-viewer-with-alpinejs)
* [Upload image to Blob Storage with VSCode - App Service/Cosmos DB - Azure | Microsoft Docs](https://docs.microsoft.com/en-us/azure/developer/javascript/tutorial/browser-file-upload-azure-storage-blob)
* [GitHub - pqina/filepond: 🌊 A flexible and fun JavaScript file upload library](https://github.com/pqina/filepond)
* [Image upload and view with pure JavaScript - simple and easy](https://codepen.io/TayabRaza/pen/VgOMMj)
* [File upload input](https://codepen.io/aaronvanston/pen/yNYOXR)
* [React Image Preview & Upload](https://codepen.io/hartzis/pen/VvNGZP)  


Previous Article: 
[https://dev.to/atherdon/saving-millions-of-images-series-part-one-od1](https://dev.to/atherdon/saving-millions-of-images-series-part-one-od1)