Question

I'm looking to leverage RackSpace's CloudFiles platform for large object storage (word docs, images, etc). Following some of their guides, I found a useful code snippet, that looks like it should work, but doesn't in my case.

    Iterable<Module> modules = ImmutableSet.<Module> of(
            new Log4JLoggingModule());
    Properties properties = new Properties();
    properties.setProperty(LocationConstants.PROPERTY_ZONE, ZONE);
    properties.setProperty(LocationConstants.PROPERTY_REGION, "ORD");
    CloudFilesClient cloudFilesClient = ContextBuilder.newBuilder(PROVIDER)
            .credentials(username, apiKey)
            .overrides(properties)
            .modules(modules)
            .buildApi(CloudFilesClient.class);

The problem is that when this code executes, it tries to log me in the IAD (Virginia) instance of CloudFiles. My organization's goal is to use the ORD (Chicago) instance as primary to be colocated with our cloud and use DFW as a back up environment. The login response results in the IAD instance coming back first, so I'm assuming JClouds is using that. Browsing around, it looks like the ZONE/REGION attributes are ignored for CloudFiles. I was wondering if there is any way to override the code that comes back for authentication to loop through the returned providers and choose which one to login to.

Update:

The accepted answer is mostly good, with some more info available in this snippet:

    RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift = cloudFilesClient.unwrap();
    CommonSwiftClient client = swift.getApi();
    SwiftObject object = client.newSwiftObject();

    object.getInfo().setName(FILENAME + SUFFIX);
    object.setPayload("This is my payload."); //input stream.
    String id = client.putObject(CONTAINER, object);
    System.out.println(id);
    SwiftObject obj2 = client.getObject(CONTAINER,FILENAME + SUFFIX);
    System.out.println(obj2.getPayload());
Was it helpful?

Solution

We are working on the next version of jclouds (1.7.1) that should include multi-region support for Rackspace Cloud Files and OpenStack Swift. In the meantime you might be able to use this code as a workaround.

private void uploadToRackspaceRegion() {
  Iterable<Module> modules = ImmutableSet.<Module> of(new Log4JLoggingModule());
  String provider = "swift-keystone"; //Region selection is limited to swift-keystone provider
  String identity = "username";
  String credential = "password";
  String endpoint = "https://identity.api.rackspacecloud.com/v2.0/";
  String region = "ORD";

  Properties overrides = new Properties();
  overrides.setProperty(LocationConstants.PROPERTY_REGION, region);
  overrides.setProperty(Constants.PROPERTY_API_VERSION, "2");

  BlobStoreContext context = ContextBuilder.newBuilder(provider)
        .endpoint(endpoint)
        .credentials(identity, credential)
        .modules(modules)
        .overrides(overrides)
        .buildView(BlobStoreContext.class);
  RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift = context.unwrap();
  CommonSwiftClient client = swift.getApi();

  SwiftObject uploadObject = client.newSwiftObject();
  uploadObject.getInfo().setName("test.txt");
  uploadObject.setPayload("This is my payload."); //input stream.

  String eTag = client.putObject("jclouds", uploadObject);
  System.out.println("eTag = " + eTag);

  SwiftObject downloadObject = client.getObject("jclouds", "test.txt");
  System.out.println("downloadObject = " + downloadObject.getPayload());

  context.close();
}

Use swift as you would Cloud Files. Keep in mind that if you need to use Cloud Files CDN stuff, the above won't work for that. Also, know that this way of doing things will eventually be deprecated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top