Reputation: 611
I'm making a Spring web app and I want my user to be able to upload some images so they could be displayed on the index page. What folder is the best to save the images in?
My app has the following structure:
MyAppRoot
|-src
|-main
|-java
|-com
|-myapp
|-package
|-<Java classes>
|-resources
|-static
|-<html pages, js controllers and services...>
|-templates
Edit: Currently I don't have any preferences regarding the saving location. It could be in the project folder, in some other location on the disk (which means it would be a server's disk when I deploy my app), as a blob in my DB (I'm using MySQL), on some third party file sharing service or on some other location. So if you can give me some pros & cons, that would be great.
Upvotes: 0
Views: 1416
Reputation: 2191
The way you will store your files will depend on the target deployment environment.
In such case, you can store your file directly on the hard drive. Things to take into account would be the path and permissions. I would put the directory in configuration. Then during deployment you would need to make sure that your app has read and write access to that folder and that the folder exists. Depending on the number of files you will store it might be important to take into account sub folders, unique naming, etc.
The disadvantage of that solution is that moving your application to cloud will most likely require change as cloud solutions typically don't allow to store files on disk permanently.
Of course, if you deploy your app on bare metal you can still use cloud services to store your images.
Here you will probably have two choices depending on the cloud provider.
You could store your files in dedicated storage like Amazon S3 mentioned by @maneesh, blobstore on Google App Engine, etc. The advantage of that solution is that these services usually provide backups, access control, allow you to access the files from many applications. One thing to take into account is pricing.
This option is what you mentioned in your question. You could store your files as blobs in MySQL, you could use GridFS if you use Mongo, etc. Implementing that should be fairly easy and comparing to the dedicated file storage solution it won't require handling additional service - you already have a database. Again thing to consider is pricing - dedicated file storage might be cheaper than additional storage for the database.
You could also use a dedicated image service like Cloudinary. On top of storage, they provide additional features like image mantipulation (resizing, cropping, retouching, etc.).
Upvotes: 1