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?

有帮助吗?

解决方案 2

jclouds does not provide a direct mechanism for this. Instead you can call BlobStore.list, compare the container names, then consume StorageMetadata.getLocation:

for (StorageMetadata resourceMd : blobStore.list()) {
    if (containerName.equals(resourceMd.getName())) {
        System.out.println(resourceMd.getLocation().getId());
    }
}

其他提示

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top