我希望利用RackSpace的CloudFiles平台进行大型对象存储(word文档,图像等)。在他们的一些指南之后,我找到了一个有用的代码片段,它看起来应该可以工作,但在我的情况下却不行。
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);
问题是,当此代码执行时,它会尝试在CloudFiles的IAD(弗吉尼亚州)实例中登录。我的组织的目标是使用ORD(芝加哥)实例作为主要与我们的云共存并使用DFW作为备份环境。登录响应导致IAD实例首先返回,所以我假设JClouds正在使用它。浏览时,CloudFiles会忽略ZONE / REGION属性。我想知道是否有任何方法可以覆盖返回的身份验证代码,以循环返回的提供程序并选择要登录的代码。
更新
接受的答案大部分都是好的,此片段中提供了更多信息:
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());
答案 0 :(得分:2)
我们正在开发下一版本的jclouds(1.7.1),其中应包括对Rackspace Cloud Files和OpenStack Swift的多区域支持。与此同时,您可以将此代码用作解决方法。
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();
}
像使用云文件一样使用swift
。请记住,如果您需要使用Cloud Files CDN的东西,上面的内容将不起作用。另外,要知道这种做事方式最终会被弃用。