Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rackspace Cloud Files (using jclouds) - how to get container location

I use jclouds as an api for Rackspace Cloud Files.

Api allows me to create containers in different locations using BlobStore.createContainerInLocation

Now, having container that is already exists, how do I get it's location?

like image 584
Eugene Loy Avatar asked Nov 01 '22 00:11

Eugene Loy


1 Answers

You can iterate over the Rackspace regions to get the Cloud Files endpoints, and then you can query each endpoint to see if the container exists there. Something like the following:

package org.jclouds.examples.rackspace.cloudfiles;

import static org.jclouds.examples.rackspace.cloudfiles.Constants.PROVIDER;

import java.io.IOException; import java.util.Set;

import org.jclouds.ContextBuilder; 
import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext; 
import org.jclouds.blobstore.BlobStore;

public class GetRegion{
  private final RegionScopedBlobStoreContext ctx;
  private final String YOUR_CONTAINER = "YOUR_CONTAINER_HERE";
  public static void main(String[] args) throws IOException {
    GetRegion getRegion = new GetRegion(args[0], args[1]);
    try {
      getRegion.getRegion();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public GetRegion(String username, String apiKey) {
    ctx = ContextBuilder.newBuilder(PROVIDER)
          .credentials(username, apiKey)
          .buildView(RegionScopedBlobStoreContext.class);
  }

  private void getRegion() {
    Set<String> regions = ctx.configuredRegions();
    for(String region:regions){
      BlobStore store = ctx.blobStoreInRegion(region);
      if(store.containerExists(YOUR_CONTAINER)) {
        System.out.format("Container is in %s region\n", region);
      }
    }
  } 
}

To run, replace "YOUR_CONTAINER_HERE" with the name of the container and pass your Rackspace username and API key as command-line arguments (alternatively, hard-code them in for 'args[0]' and 'args[1]', respectively).

like image 53
JRP Avatar answered Nov 15 '22 03:11

JRP