Guides

Storage OnLine: Using S3 APIs with Java and JSPs

The S3 REST API programming interfaces were created by Amazon for its cloud storage service (Simple Storage Service - S3). As they are public specifications and are based on REST-type web services, it is very easy to create clients. To use the S3 API with the Java language and JSP, (as in the case of PHP) , we essentially have two choices:

AWS SDK for Java is Amazon's official library for the support of all its services; it is therefore very complex and has a large number of features that are not needed in this example. Jets3t Toolkit on the other hand is focused (as the name suggests) only on the S3 API, which is why it will be used for the examples.

Sample web application

This is a Java web application created using only JSP page and inserting the code directly into the pages. This style of programming has not been in use in the enterprise environment for a long time, preferring (rightly) the use of MVC, JSF, or other frameworks. However, its simplicity allows you to concentrate on the operation of the S3 API. To make things neater, but above all to encourage reuse of the code in other projects, we have also used the JSTL (Java Standard Tag Libraries).

The structure of the web application is as follows:

  • / : root directory of the web application, containing the JSP pages;
  • /WEB-INF : directory not accessible from the web, normally used for configuration files;
  • /WEB-INF/web.xml : configuration of the web application;
  • /WEB-INF/lib : libraries used by the application;
  • /WEB-INF/includes : directory containing JSP fragments to be included and reused in various parts of the application. As they are stored in /WEB-INF, they are not directly accessible from the browser.

Configurazione e deploy

The first step now is to configure the web application so that it can connect to the Hosting Solutions Storage OnLine space.

Il punto di accesso S3 è:
#ACCESSO_S3#

Le credenziali di accesso sono:
ID chiave di accesso: #USERNAME#
Chiave di accesso segreta: #PASSWORD#
      

Please note. Instead of #ACCESSO_S3#, #AUSERNAME# and #PASSWORD# you will obviously need to enter the correct values, which are those displayed in the Gestione Storage (Storage Management) tab of the control panel, in the section regarding the activation of the S3 protocol; these data can also be found in the email that will be sent when the S3 protocol is activated from the control panel.

These values must be entered in the in the /WEB-INF/web.xml file:

[...]

<context-param>
    <param-name>endpoint</param-name>
    <param-value>#ENDPOINT#</param-value>
  </context-param>
  <context-param>
    <param-name>accessKey</param-name>
    <param-value>#ACCESS_KEY#</param-value>
  </context-param>
  <context-param>
    <param-name>secretKey</param-name>
    <param-value>#SECRET_KEY#</param-value>
  </context-param>

[...]
      

Once these values have been entered, we are ready to deploy. The application is compatible with the Servlet 2.5 specification, so it can also be used on Tomcat 6.x o Higher, which is the servlet engine available on our Hosting Platinum Linux and Hosting Enterprise Linux plans.

It is possible to deploy in two ways:

  • WAR file. In this case you must:
    • create an archive hsstorage.zip containing the files of the web application. Attention: the archive must have the same structure as the root folder seen above, that is, WEB-INF and the index.jsp file must be on the first level;
    • change the file extension to .war (Web Application aRchive);
    • copy the file hsstorage.war inside Tomcat's webapps folder;
    • Start Tomcat (the server will automatically expand the archive);
  • folder in webapps. In this case you need to:
    • create a folder hsstorage inside webapps under Tomcat;
    • copy all the files of the web application inside this folder;
    • start Tomcat.

If the application is configured correctly, it can be accessed using the following address:http://localhost:8080/hsstorage

the initial index.jsppage connects to the Storage OnLine and displays the list of available buckets:

In case of problems an error message will be displayed.

Resources: