gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:18:37 27 4
gpt4 key购买 nike

我希望利用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com