java - 覆盖 jclouds 中的身份验证行为

标签 java jclouds

我希望利用 RackSpace 的 CloudFiles 平台进行大型对象存储(文字文档、图像等)。按照他们的一些指南,我发现了一个有用的代码片段,看起来应该可以工作,但在我的情况下却不行。

    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());

最佳答案

我们正在开发 jclouds 的下一个版本 (1.7.1),其中应包括对 Rackspace 云文件和 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。请记住,如果您需要使用云文件 CDN 内容,则上述内容将不起作用。另外,请注意这种做事方式最终将被弃用。

关于java - 覆盖 jclouds 中的身份验证行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222882/

相关文章:

amazon-s3 - JClouds S3 : Specify Content Length when uploading file

java - 在 Spring MVC 应用程序中使用 jclouds 创建 BlobContext 时出错

java - 在 Spring 安全性中允许除一个之外的所有 URL

java - 在 Freemarker 中禁用解释器

java - 多个语句 if 条件为真简写 if

java - 字符串中的最后一个重复字符

java - 如何创建一个可以存储不相关类的对象的变量

java - 如何为 imageNameRegex 提供实际的正则表达式

云中的 Java 框架

rackspace - 使用 JClouds Location API 设置容器位置