简体   繁体   English

Rackspace云文件(使用jclouds) - 如何获取容器位置

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

I use jclouds as an api for Rackspace Cloud Files. 我使用jclouds作为Rackspace Cloud Files的api。

Api allows me to create containers in different locations using BlobStore.createContainerInLocation Api允许我使用BlobStore.createContainerInLocation在不同位置创建容器

Now, having container that is already exists, how do I get it's location? 现在,拥有已经存在的容器,我该如何获取它的位置?

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. 您可以遍历Rackspace区域以获取Cloud Files端点,然后您可以查询每个端点以查看容器是否存在。 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). 要运行,请将“YOUR_CONTAINER_HERE”替换为容器名称,并将Rackspace用户名和API密钥作为命令行参数传递(或者,分别将它们硬编码为'args [0]'和'args [1]' )。

jclouds does not provide a direct mechanism for this. jclouds没有为此提供直接机制。 Instead you can call BlobStore.list , compare the container names, then consume StorageMetadata.getLocation : 相反,您可以调用BlobStore.list ,比较容器名称,然后使用StorageMetadata.getLocation

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM