-
Notifications
You must be signed in to change notification settings - Fork 198
Swift Tutorial
Shiyao Qi edited this page Jun 5, 2014
·
7 revisions
Here you can find a step by step to start working on Swift.
Enjoy!
- File > New > Other > Maven Project
- Check on “Create a simple project (skip archetype selection)”
- Set the following properties
Group Id | Artifact Id | Version |
---|---|---|
org.openstack | openstack-swift-demo | 0.0.1-SNAPSHOT |
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>
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() + ")");
}
}
}
[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);
}
}