Skip to content
woorea edited this page May 16, 2012 · 7 revisions

.h1 Swift Tutorial

.h2 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("my1firstcontainer").put();

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

		account.container("my1firstcontainer").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(
				"my1firstcontainer").get();

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

	}
}

Clone this wiki locally