How to Connect and Uploading Files to Openstack using Java

Currently im involved in a project where i have to use OpenStack Object Store for storing images and files. After Googling for a while, i come up with a solution of using JOSS library to connect to OpenStack, despite OpenStack provide REST API to connect to it directly.

But first, we need to see the API URL that is needed for login to OpenStack. And it can be seen on below screenshot, inside the red box.

After we got the url, next is importing JOSS library to be used in our app. We can import it by using a simple maven command,

<dependency>
	<groupId>org.javaswift</groupId>
	<artifactId>joss</artifactId>
	<version>0.10.2</version>
</dependency>

And a simple java file, to do a simple upload using a provided credential.

@Test
public void uploadImageToDCTest() {
	AccountConfig config = new AccountConfig();
	config.setAuthenticationMethod(AuthenticationMethod.KEYSTONE_V3);
	config.setUsername("user");
	config.setPassword("password");
	config.setAuthUrl("https://osphdc.server01.com:13000/v3/auth/tokens");
	config.setTenantName("default");

	Account account = new AccountFactory(config).createAccount();

	Container container = account.getContainer("images");
	if(!container.exists()) {
		container.create();
		container.makePublic();
	}

	StoredObject object = container.getObject("jenkins-logo.png");
	object.uploadObject(new File("D:\\jenkins-logo.png"));
	System.out.println("Public URL: "+object.getPublicURL());
}

Later on, we can check the result on our OpenStack object store page.

Google+

No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)