Skip to content

Instantly share code, notes, and snippets.

@Yinying
Created November 20, 2012 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yinying/4118146 to your computer and use it in GitHub Desktop.
Save Yinying/4118146 to your computer and use it in GitHub Desktop.
import java.util.Properties;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.options.CreateContainerOptions;
import org.jclouds.rest.RestContext;
import org.jclouds.openstack.swift.domain.ContainerMetadata;
import com.google.common.collect.ImmutableMap;
/**
* Create an object storage container with some metadata associated with it.
*
* @author Everett Toews
*/
public class JCloudsSwift{
private static final String CONTAINER = "jclouds-example";
private BlobStore storage;
private RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift;
/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
*/
public static void main(String[] args) {
JCloudsSwift createContainer = new JCloudsSwift();
try {
createContainer.init(args);
// createContainer.createContainer();
}
finally {
createContainer.close();
}
}
private void init(String[] args) {
// The provider configures jclouds to use the Rackspace open cloud (US)
// to use the Rackspace open cloud (UK) set the provider to "cloudfiles-uk"
Properties properties = new Properties();
properties.setProperty("swift-keystone.endpoint", "http://166.78.4.64:5000/v2.0");
properties.setProperty("jclouds.keystone.credential-type",
"passwordCredentials");
properties.setProperty("jclouds.identity", "demo:demo");
properties.setProperty("jclouds.credential", "devstack");
BlobStoreContext context = ContextBuilder.newBuilder("swift-keystone").overrides(properties).build(BlobStoreContext.class);
BlobStore blobStore = context.getBlobStore();
blobStore.createContainerInLocation(null, CONTAINER);
storage = context.getBlobStore();
swift = context.unwrap();
}
private void createContainer() {
System.out.println("Create Container");
CreateContainerOptions options = CreateContainerOptions.Builder
.withMetadata(ImmutableMap.<String, String> of(
"key1", "value1",
"key2", "value2"));
swift.getApi().createContainer(CONTAINER, options);
System.out.println(" " + CONTAINER);
}
/**
* Always close your service when you're done with it.
*/
private void close() {
if (storage != null) {
storage.getContext().close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment