Henrique
Henrique

Reputation: 5011

Locally store and serve user-uploaded image files from a web app

I have a play app that handles user-uploaded image files. The app will have to constantly serve those image files to different users (think of it as a really simple version of a instagram).

Since this is a prototype, I've decided to keep costs down and instead of using a service like Amazon S3, simply store and serve those image files locally on the server for now.

My question is, how can I store these images files in a location inside a ubuntu server which will remain intact when I roll out upgrades to this play web app? In the past, I've used symbolic links inside the web app's parent folder so that the webapp itself could simply write to a local folder inside the web app director (eg. /images) and that would be a link to something like /var/images, but something tells me this isn't the best way to do things.

Any recommendations? Thanks.

Upvotes: 0

Views: 322

Answers (2)

biesior
biesior

Reputation: 55798

For this scenario I always suggest to use additional HTTP server for serving the images (and other static assets even your app's styles and graphics) it always take less resorces than Play, and probably will be little bit faster as you don't need to rewrite the image every time to the browser with the controller.

Then Play just keeps the paths to the file on the other server and renders the main app view.

When you'll use separate domain (not only subdomain) you can also save some money on transfers, as browser won't send the cookies.

Additional benefit is that you can move the files between different machines (ie. Amazon, or some proffesional CDNs) and all you need is just to change the path in DB during the move.

Upvotes: 0

grotrianster
grotrianster

Reputation: 2468

You can store images at any location, you just have to create Action to serve those assets from different location, like this:

object Images extends Controller  {

  def getImage( file: String) = Action   {
    val image = new     File(Play.current.configuration.getString("images.directory").getOrElse("")+File.separator+file)
    Ok.sendFile(image)
  }

} 

images.directory is a property which points to the directory in file system, it is defined in application.conf file

add new resource to routes file

GET     /images/*file               controllers.Images.getImage(file:String)

Upvotes: 2

Related Questions