JOSS application

Here's the same JOSS application.

/**
* This sample Java application shows how to use the HCP
*HSwift API, which is compatible with JOSS SDK. The
*application uses the JOSS SDK.
*/

 

package com.hds.hds.hcp.examples;

 

import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.javaswift.joss.model.StoredObject;

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class HSwiftSampleApp {

 

/**

* @param args

*/

 

public static void main(String[] args) {

 

/*

* Initialize access credentials for the HSwift client.

*/

 

// base64 of HCP user name: "lgreen"

String accessKey = "bGdyZWVu";

// md5 of HCP user password: "p4ssw0rd"

String secretKey = "2a9d119df47ff993b662a8ef36f9ea20";

 

/*

* Build the JOSS account connection to be used

* for communication with HCP. The Keystone must have *an admin user lgreen with password p4ssw0rd and *tenant europe. The AuthUrl must point to the keystone *server.

*/

 

AccountConfig accountConfig = new AccountConfig();

accountConfig.setAuthUrl("http://api.keystone.server.com:5000/v2.0/token");

accountConfig.setUsername(accessKey);

accountConfig.setPassword(secretKey);

accountConfig.setAuthenticationMethod(AuthenticationMethod.KEYSTONE);

Account europe = new AccountFactor(accountConfig).createAccount();

 

/*

* Now that the HSwift Client is created for HCP

*usage, proceed with some operations.

*/

 

String containerName = "finance";

try {

 

/*
* Create a new container. With HCP, the container *name does not need to be globally unique. It needs *to be unique only within the HCP service point *that is, the HCP tenant.
*/

 

System.out.println("Creating container " + containerName + "\n");
Container finance = europe.getContainer(containerName);
finance.create();

 

/*
* List the containers you own at the service point.
*/

 

System.out.println("Containers:");
for (Container container : europe.list()) {
System.out.println(" * " + container.getName());
}

 

System.out.println();

 

/*
* Add an ACL to the container to give read to a user with the specified user name.
*/


finance.setContainerRights(null, "pdgray");

 

/*
* Upload a couple of objects to the container from *files on the local file system.
*/

 

String firstFileName = "input/Q4_2012.ppt";
String firstObjectName = "quarterly_rpts/Q4_2012.ppt";
StoredObject Q4_2012 = finance.getObject(firstObjectName);

 

System.out.println("Uploading first object to HCP from a file\n");

Q4_2012.uploadObject(new FileInputStream(firstFileName));

 

// Add metadata for first object
Q4_2012.setAndSaveMetadata("Author", "P.D. Gray");
Q4_2012.setAndSaveMetadata("Audit_Date", "2013-02-23");

 

// Write a second object without metadata.
System.out.println("Uploading second object to HCP from a file\n");
String secondFileName = "input/Q3_2012.ppt";
String secondObjectName = "quarterly_rpts/Q3_2012.ppt";
StoredObject Q3_2012 = finance.getObject(secondObjectName);
Q3_2012.uploadObject(new FileInputStream(secondFileName));

 

/*
* List objects in the container with prefix *quarterly_rpts/Q. The objects listing is limited *to 1,000 items per request.
*/


System.out.println("Objects:");
for (StoredObject object : finance.list("quarterly_rpts/Q", null, 1000)) {
     System.out.println(" * " + object.getName() + " " +
          "(size = " + object.getContentLength() + ")");
}

 

System.out.println();

 

/*
* Download an object. When you download an object, * you get all the object metadata and a stream from * which to read the object content.
*/

 

System.out.println("Downloading the first object\n");
InputStream responseStream = Q4_2012.downloadObjectAsInputStream();

 

// Write the content to a file named Q4_2012.ppt in the output folder.
FileOutputStream dataFile = new FileOutputStream("output/Q4_2012.ppt");
// Keep reading bytes until the end of stream is reached.
byte buffer[] = new byte[2048];
int readSize;
while (-1 != (readSize = responseStream.read(buffer))) {
     dataFile.write(buffer, 0, readSize);
}
dataFile.close();

 

/*
* Add an ACL to the container which gives full control to the rsilver account.
*/

 

finance.setContainerRights("rsilver", "rsilver");

 

/*
* Perform a download of the second object.
*/

 

System.out.println("Checking the second object");
responseStream = Q3_2012.downloadObjectAsInputStream();

 

// Download it to a new file.
System.out.println("Downloading new revision\n");
FileOutputStream dataFile2 = new FileOutputStream("output/Q3_2012_Rev2.ppt");
// Keep reading bytes until the end of stream is reached.
byte readBuffer[] = new byte[2048];
int conditionalReadSize;
while (-1 != (conditionalReadSize
          = responseStream.read(readBuffer))) {
     dataFile2.write(readBuffer, 0, conditionalReadSize);
}

dataFile2.close();

 

/*
* Delete the objects.
*/

 

System.out.println(
     "Deleting the objects created by this sample application\n");
Q4_2012.delete();
Q3_2012.delete();

 

/*
* Delete the folder.
*/

 

System.out.println(
          "Deleting the folder created when the first object was stored\n");
StoredObject folder = finance.getObject("quarterly_rpts");
folder.delete();

 

/*
* Delete the container.
*/

 

System.out.println("Deleting the finance container\n");
finance.delete();

 

} catch (IOException ioe) {
     System.out.println(
          "Caught an IOException while trying to create an object or read "
               + "from an internal buffer.");
     System.out.println("Error Message: " + ioe.getMessage());
}

}

}

Trademark and LegalDisclaimer

© 2016 Hitachi Data Systems Corporation. All rights reserved.