Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
woorea edited this page May 25, 2012 · 6 revisions

Welcome to the OpenStack Java SDK tutorial.

Here you can find a step by step to start working on Swift.

Enjoy!

Setting up Eclipse Project

  1. File > New > Other > Maven Project
  2. Check on “Create a simple project (skip archetype selection)”
  3. Set the following properties
Group Id Artifact Id Version
org.openstack openstack-swift-demo 0.0.1-SNAPSHOT

Configure Maven

Edit your pom.xml (it should look similar to the one below)

<?xml version="1.0"?>
<project 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.openstack</groupId>
  <artifactId>openstack-swift-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>OpenStack Java SDK for Swift</name>
  <dependencies>
    <dependency>
      <groupId>org.openstack</groupId>
      <artifactId>openstack-java-sdk</artifactId>
      <version>1.0-RC2</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>woorea-releases</id>
      <url>https://github.com/woorea/maven/raw/master/releases</url>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

h2. Write the HelloWorld class
package org.openstack.demos.swift;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import org.openstack.api.storage.AccountResource;
import org.openstack.client.OpenStackClient;
import org.openstack.model.storage.swift.SwiftStorageObject;
import org.openstack.model.storage.swift.SwiftStorageObjectProperties;

public class TestingSwift {

  public static void main(String[] args) {

    	Properties properties = System.getProperties();
	properties.put("verbose", "true");
	properties.put("auth.credentials", "passwordCredentials");
	properties.put("auth.username", "demo");
	properties.put("auth.password", "secret0");
	properties.put("auth.tenantName", "demo");
	properties.put("identity.endpoint.publicURL","http://192.168.1.43:5000/v2.0");

	OpenStackClient openstack = OpenStackClient.authenticate(properties);

	AccountResource account = openstack.getStorageEndpoint();

	account.container("hellocontainer").put();

	account.container("hellocontainer").object("dir1").put();

	account.container("hellocontainer").object("test1")
		.put(new File("pom.xml"), new SwiftStorageObjectProperties() {{
		  setContentType("application/xml");
		  getCustomProperties().putAll(new HashMap<String, String>() {{
		    put("customkey.1", "customvalue.1");
                  }});
		}});

	List<SwiftStorageObject> objects = account.container("hellocontainer").get();

	for (SwiftStorageObject o : objects) {
		System.out.println(o.getName() + " (" + o.getContentType() + ")");
	}
  }
}

HPCloud using the StorageClient (high level api)

[to try the code below you have to build from source, RC3 not releleased yet]

package org.openstack.demos;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

import org.openstack.client.OpenStackClient;
import org.openstack.client.StorageClient;

public class TestingSwiftClient {
	
	public static final File TEST_FILE = new File("<your_file_path>");
	
	public static final String TEST_FILE_CONTENT_TYPE = "<your_file_content_type>";
	
	public static final String TEST_CONTAINER = "test-container";
	
	public static final String TEST_DIRECTORY = "test-directory";
	
	public static final String TEST_SUBDIRECTORY = "test-directory/test-subdirectory";
	
	public static final String TEST_OBJECT = "test-object";
	
	public static final String TEST_SUBDIRECTORY_OBJECT = "test-directory/test-subdirectory/test-object2";

	public static void main(String[] args) throws IOException {

		Properties properties = System.getProperties();
		properties.put("verbose", "true");
		properties.setProperty("auth.credentials", "apiAccessKeyCredentials");
		properties.setProperty("auth.accessKey", "<your_hpcloud_access_key>");
		properties.setProperty("auth.secretKey", "<your_hpcloud_secret_key>");
		properties.setProperty("auth.tenantId", "<your_hpcloud_tenant_id>");
		properties.put("identity.endpoint.publicURL","<your_hpclud_identity_endpoint>");

		StorageClient swift = OpenStackClient.authenticate(properties).getStorageClient();
		swift.createContainer(TEST_CONTAINER);
		
			swift.createDirectory(TEST_CONTAINER, TEST_DIRECTORY);
			
				swift.createObject(TEST_CONTAINER, TEST_OBJECT, TEST_FILE,
                                new HashMap<String, String>() {{
					put("Content-Type", TEST_FILE_CONTENT_TYPE);
				}});
				
				swift.getObjectAsString(TEST_CONTAINER, TEST_OBJECT);
				
				swift.createDirectory(TEST_CONTAINER, TEST_SUBDIRECTORY);
				
					swift.createObject(TEST_CONTAINER, TEST_SUBDIRECTORY_OBJECT, TEST_FILE);
					
					swift.getObjectAsString(TEST_CONTAINER, TEST_OBJECT);

					swift.deleteObject(TEST_CONTAINER, TEST_SUBDIRECTORY_OBJECT);
				
				swift.deleteObject(TEST_CONTAINER, TEST_SUBDIRECTORY);
				
				swift.deleteObject(TEST_CONTAINER, TEST_OBJECT);
			
			swift.deleteObject(TEST_CONTAINER, TEST_DIRECTORY);
		
		swift.deleteContainer(TEST_CONTAINER);
		
	}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.