How to get stored images/videos from Object Storage in IBM Bluemix

screen-shot-2016-09-06-at-9-56-25-pm

What is Object Storage?

IBM® Object Storage for Bluemix® provides you with access to a fully provisioned Swift Object Storage account to manage your data. Swift provides a fully distributed, API-accessible storage platform. You can use it directly in your applications or for backups, making it ideal for cost effective, scale-out storage.

This blog will help you to get images/videos from Cloud Data Storage(Object Storage) into your application.

Prerequisites

Below shown is the UI of Object Storage. I have 4 images & 1 video uploaded to a container named store1.

screen-shot-2016-09-06-at-10-01-03-pm

Front end of the application:

screen-shot-2016-09-06-at-10-05-31-pm

Code to get the image/video from Object Storage

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

String imgoption = request.getParameter(“imgOpt”); //Takes the user input

String envApp = System.getenv(“VCAP_APPLICATION”);

String envServices = System.getenv(“VCAP_SERVICES”);

System.out.print(envServices);

JSONParser parser = new JSONParser();

try{

Object obj = parser.parse(envServices);

JSONObject jsonObject = (JSONObject) obj;

JSONArray vcapArray = (JSONArray) jsonObject.get(“Object-Storage”);

JSONObject vcap = (JSONObject) vcapArray.get(0);

JSONObject credentials = (JSONObject) vcap.get(“credentials”);

String userId = credentials.get(“userId”).toString();

String password = credentials.get(“password”).toString();

String auth_url = credentials.get(“auth_url”).toString() + “/v3”;

String domain = credentials.get(“domainName”).toString();

String project = credentials.get(“project”).toString();

Identifier domainIdent = Identifier.byName(domain);

Identifier projectIdent = Identifier.byName(project);

OSClientos = OSFactory.builderV3()

.endpoint(auth_url)

.credentials(userId, password)

.scopeToProject(projectIdent, domainIdent)

.authenticate();

System.out.print(os);

SwiftAccount account = os.objectStorage().account().get();

ObjectStorageService objectStorage = os.objectStorage();

List<? extends SwiftContainer> containers = os.objectStorage().containers().list();

System.out.println(containers); // Prints the list of containers

List<? extends SwiftObject> objs = os.objectStorage().objects().list(“store1”);

System.out.println(obj); //Prints the list of objects inside the container store1

SwiftObject fileObj = objectStorage.objects().get(“store1”,imgoption);

System.out.println(fileObj);

String mimeType = fileObj.getMimeType();

DLPayload payload = fileObj.download();

InputStream in = payload.getInputStream();

response.setContentType(mimeType);

OutputStream out = response.getOutputStream();

IOUtils.copy(in, out);

in.close();

out.close();

}catch(Exception e){

e.printStackTrace();

}

}

Ensure that you have added the dependency to POM.XML file

  <dependencies>

  <dependency>

  <groupId>org.pacesys</groupId>

  <artifactId>openstack4j</artifactId>

  <version>3.0.0</version>

</dependency>

<dependency>

    <groupId>org.apache.directory.studio</groupId>

    <artifactId>org.apache.commons.io</artifactId>

    <version>2.4</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

<scope>provided</scope>

</dependency>

<dependency>

    <groupId>com.googlecode.json-simple</groupId>

    <artifactId>json-simple</artifactId>

    <version>1.1</version>

</dependency>

</dependencies>



<build>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>3.1</version>

            <configuration>

                <source>1.8</source>

                <target>1.8</target>

            </configuration>

        </plugin>

    </plugins>

  </build>

 

Please note that OpenStack4j has some of the same libraries that are also provided in the Liberty profile if you are running this Java application on Bluemix. To avoid this you should use reverse class loading or exclude jaxrs from the enabled features. 

This is a Java app which was deployed on a Node.JS runtime.

Output of the app:

Video & Image