Skip to content

Instantly share code, notes, and snippets.

@etoews
Created January 3, 2013 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save etoews/4446492 to your computer and use it in GitHub Desktop.
Save etoews/4446492 to your computer and use it in GitHub Desktop.
import static com.google.common.io.Closeables.closeQuietly;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.domain.SwiftObject;
import org.jclouds.openstack.swift.options.CreateContainerOptions;
import org.jclouds.rest.RestContext;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
public class JCloudsSwift implements Closeable {
private static final String CONTAINER = "jclouds";
private BlobStore storage;
private RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift;
public static void main(String[] args) {
JCloudsSwift jCloudsSwift = new JCloudsSwift();
try {
jCloudsSwift.init();
jCloudsSwift.createContainer();
jCloudsSwift.createObjectFromFile();
jCloudsSwift.createObjectFromString();
jCloudsSwift.createObjectFromStringWithMetadata();
jCloudsSwift.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
jCloudsSwift.close();
}
}
private void init() {
Iterable<Module> modules = ImmutableSet.<Module> of(
new SLF4JLoggingModule());
String provider = "swift-keystone";
String identity = "demo:demo"; // tenantName:userName
String password = "devstack"; // demo account uses ADMIN_PASSWORD too
BlobStoreContext context = ContextBuilder.newBuilder(provider)
.endpoint("http://172.16.0.1:5000/v2.0/")
.credentials(identity, password)
.modules(modules)
.buildView(BlobStoreContext.class);
storage = context.getBlobStore();
swift = context.unwrap();
}
private void createContainer() {
System.out.println("Create Container");
CreateContainerOptions options = CreateContainerOptions.Builder
.withMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"));
swift.getApi().createContainer(CONTAINER, options);
System.out.println(" " + CONTAINER);
}
/**
* Create an object from a File using the Swift API.
*/
private void createObjectFromFile() throws IOException {
System.out.println("Create Object From File");
String filename = "createObjectFromFile";
String suffix = ".txt";
File tempFile = File.createTempFile(filename, suffix);
tempFile.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
out.write("createObjectFromFile");
out.close();
SwiftObject object = swift.getApi().newSwiftObject();
object.getInfo().setName(filename + suffix);
object.setPayload(tempFile);
swift.getApi().putObject(CONTAINER, object);
System.out.println(" " + filename + suffix);
}
/**
* Create an object from a String using the Swift API.
*/
private void createObjectFromString() {
System.out.println("Create Object From String");
String filename = "createObjectFromString.txt";
SwiftObject object = swift.getApi().newSwiftObject();
object.getInfo().setName(filename);
object.setPayload("createObjectFromString");
swift.getApi().putObject(CONTAINER, object);
System.out.println(" " + filename);
}
/**
* Create an object from a String with metadata using the BlobStore API.
*/
private void createObjectFromStringWithMetadata() {
System.out.println("Create Object From String With Metadata");
String filename = "createObjectFromStringWithMetadata.txt";
Map<String, String> userMetadata = new HashMap<String, String>();
userMetadata.put("key1", "value1");
Blob blob = storage.blobBuilder(filename)
.payload("createObjectFromStringWithMetadata")
.userMetadata(userMetadata)
.build();
storage.putBlob(CONTAINER, blob);
System.out.println(" " + filename);
}
public void close() {
closeQuietly(storage.getContext());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment