Search code examples
javaamazon-ec2amazon-web-servicesamazon-ebsservlet-3.0

Read a file from Amazon EBS


I want to download a file from one of the EBS volumes I created on Amazon Elastic block storage. Mostly it is advisable to used ServletContext#getResource() and its counterpart ServletContext#getResourceAsStream() as well advised here.

But in this case is the following code advisable

InputStream in = new FileInputStream(new File(FOLDER_PATH_ON_AMAZON_EBS + "/" + folder + "/" + fileName));


Solution

  • It's hard to tell what the question is here.

    If you are asking whether it is better to use getServletContext() or new File(PATH_TO_EBS...) then it simply depends on what you are running. If you are running a standalone java application and requesting files via sockets, then you would use the latter (a FileInputStream over a file you know where to look). If you are running a web server (eg Tomcat) and will be using a web client to download the file, then you would typically use the getServletContext() since that is part of the web-server infrastructure.

    Both ways let you get a handle on the file, but getServletContext() is going to refer to a location for your application under Tomcat's working area. Are you going to mount your EBS volume somewhere where you can easily reach it starting from Tomcat's working area.

    If you are running a web server and it is allowing you to reach a file directly in your EBS volume with new FileInputStream(new File(MY_EBS_LOCATION + "/" + ...)) then use it by all means - clear and easy.