Overview

Skill Level: Any Skill Level

Developers wanting to build an application that utilizes Object Storage to maintain their unstructured data.

Ingredients

  • Familiarity with Java development
  • A Bluemix account
  • A Bluemix project and Object Storage service
  • CF CLI installed
  • OpenStack4j

Step-by-step

  1. Create a Java application on Bluemix

    1. Navigate to your dashboard and select create app
    2. Select web application and Liberty for Java as the runtime
    3. Once you’ve named your web application, download the started code and the CF CLI if you have not already done so

  2. Create and bind an Object Storage instance

    1. Navigate back to the dashboard and select User Services or APIs , this will take you to the Catalog
    2. In the catalog under Storage, select Object Storage
      1. From here you will have the option to bind your Object Storage Instance to an application. Select the Java application you created in step one as the bound application.
    3. Your application will now be restaged. Once that is finished you will be ready to pull credentials for your service.
  3. Get credentials for the Object Storage instance using Java

    1. In you dashboard, select your Java application
    2. Navigate to the Environment Variables section using the tabs on the left side of the screen.
    3. You will see a JSON object for VCAP Credentials, these are the credentials that you will be using along with OpenStack4j to object storage in your application.

  4. Connect to Object Storage using OpenStack4j

    This is intended to show a basic way to connect to object storage via Java.You can implement this in any type of Java application as long as you’ve included the OpenStack4j library. This requires you to download a JAR file for the OpenStack4j library or include it through your package manager of choice.

    Note: When using OpenStack4j you may encounter a class-loading issue with 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.

    VCAP credentials can be accessed from the environment of your Bluemix applications. This is true for any runtime, not just Java. The credentials accessed programmatically should match the credentials displayed on your Bluemix dashboard.

     String envApp = System.getenv("VCAP_APPLICATION");
    String envServices = System.getenv("VCAP_SERVICES");
    JSONParser parser = new JSONParser();

    Parse the credentials from the VCAP JSON string. Note that two identifiers are created using the name of our domain and project.

     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);

    OSFactory.builderV3() is a factory that sets up a new or non expired authorization for the openstack apis to use. It requires four of the credentials pulled from the previous step and shown below for authorization.

     OSClient os = OSFactory.builderV3()
    .endpoint(auth_url)
    .credentials(userId, password)
    .scopeToProject(projectIdent, domainIdent)
    .authenticate();

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

    At this point you are fully connected to your object storage instance. For more information on the capabilities of Object Stoarge with OpenStack4J read the documentation here

Join The Discussion