[英]Rackspace Cloud Files (using jclouds) - how to get container location
我使用jclouds作為Rackspace Cloud Files的api。
Api允許我使用BlobStore.createContainerInLocation在不同位置創建容器
現在,擁有已經存在的容器,我該如何獲取它的位置?
您可以遍歷Rackspace區域以獲取Cloud Files端點,然后您可以查詢每個端點以查看容器是否存在。 類似於以下內容:
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);
}
}
}
}
要運行,請將“YOUR_CONTAINER_HERE”替換為容器名稱,並將Rackspace用戶名和API密鑰作為命令行參數傳遞(或者,分別將它們硬編碼為'args [0]'和'args [1]' )。
jclouds沒有為此提供直接機制。 相反,您可以調用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.