Java jclouds中的重写身份验证行为

Java jclouds中的重写身份验证行为,java,jclouds,Java,Jclouds,我希望利用RackSpace的CloudFiles平台进行大对象存储(word文档、图像等)。按照他们的一些指导,我发现了一个有用的代码片段,看起来应该可以工作,但在我的例子中却不行 Iterable<Module> modules = ImmutableSet.<Module> of( new Log4JLoggingModule()); Properties properties = new Properties();

我希望利用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);
Iterable模块=不可变表集。的(
新的Log4JLoggingModule());
属性=新属性();
properties.setProperty(LocationConstants.PROPERTY\u区域,区域);
properties.setProperty(LocationConstants.PROPERTY_REGION,“ORD”);
CloudFileClient CloudFileClient=ContextBuilder.newBuilder(提供程序)
.凭据(用户名、apiKey)
.覆盖(属性)
.模块(模块)
.buildApi(CloudFilesClient.class);
问题是,当执行此代码时,它试图将我登录到CloudFiles的IAD(Virginia)实例中。我的组织的目标是使用ORD(Chicago)实例作为主要实例,与我们的云共存,并使用DFW作为备份环境。登录响应导致IAD实例首先返回,因此我假设JClouds正在使用它。浏览四周,看起来CloudFile的区域/区域属性被忽略。我想知道是否有任何方法可以覆盖返回进行身份验证的代码,以循环返回的提供者并选择登录哪个提供者

更新:

公认的答案基本上是好的,在此片段中提供了更多信息:

    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());
RestContext swift=cloudFilesClient.unwrap();
CommonSwiftClient=swift.getApi();
SwiftObject对象=client.newSwiftObject();
object.getInfo().setName(文件名+后缀);
setPayload(“这是我的有效载荷。”)//输入流。
String id=client.putObject(容器,对象);
系统输出打印项次(id);
SwiftObject obj2=client.getObject(容器,文件名+后缀);
System.out.println(obj2.getPayload());

我们正在开发jclouds的下一个版本(1.7.1),该版本应包括对Rackspace云文件和应用程序的多区域支持。同时,您可能可以使用此代码作为变通方法

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();
}
private void uploadToRackspaceRegion(){
Iterable modules=ImmutableSet.of(新的Log4JLoggingModule());
String provider=“swift keystone”;//区域选择仅限于swift keystone provider
String identity=“username”;
字符串credential=“password”;
字符串端点=”https://identity.api.rackspacecloud.com/v2.0/";
字符串region=“ORD”;
属性覆盖=新属性();
overrides.setProperty(LocationConstants.PROPERTY_REGION,REGION);
overrides.setProperty(Constants.PROPERTY_API_版本,“2”);
BlobStoreContext上下文=ContextBuilder.newBuilder(提供程序)
.endpoint(端点)
.凭证(身份、凭证)
.模块(模块)
.覆盖(覆盖)
.buildView(BlobStoreContext.class);
RestContext swift=context.unwrap();
CommonSwiftClient=swift.getApi();
SwiftObject uploadObject=client.newSwiftObject();
uploadObject.getInfo().setName(“test.txt”);
uploadObject.setPayload(“这是我的有效负载。”);//输入流。
字符串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
就像对文件进行云计算一样。请记住,如果您需要使用云文件CDN之类的东西,上述内容将无法实现。另外,要知道这种方法最终会被弃用。

我最初尝试过这种方法,这种示例的一个问题是没有定义swift和storage变量。您还可以评论一下如何使用swift获取特定容器吗?网站评论中也没有这个。好吧,所以我玩得更进一步了。只有使用密码凭据,而不是API密钥,我才能让它工作。API键的结果是400。我将答案标记为已接受。API密钥问题仍然是个问题,但我至少可以使用用户名/密码登录。对于那些好奇的人,另一种看待存储和swift的方式是在我的原稿中。非常感谢你的反馈John。我更新了我的示例以使其完整。