![](/img/trans.png)
[英]How to set the default header for all requests in apache http client?
[英]How to set HTTP header in Apache JClouds?
我正在使用Apache JClouds连接到我的Openstack Swift安装。 我设法从Swift上载和下载对象。 但是,我看不到如何将动态大对象上传到Swift。
要上传动态大对象,我需要先上传所有片段,就像往常一样。 然后,我需要上传一个清单对象以逻辑上将它们组合起来。 问题是要告诉Swift这是一个清单对象,我需要设置一个特殊的标头,我不知道如何使用JClouds api来实现。
这是来自openstack官方网站的动态大对象示例 。
我正在使用的代码:
public static void main(String[] args) throws IOException {
BlobStore blobStore = ContextBuilder.newBuilder("swift").endpoint("http://localhost:8080/auth/v1.0")
.credentials("test:test", "test").buildView(BlobStoreContext.class).getBlobStore();
blobStore.createContainerInLocation(null, "container");
ByteSource segment1 = ByteSource.wrap("foo".getBytes(Charsets.UTF_8));
Blob seg1Blob = blobStore.blobBuilder("/foo/bar/1").payload(segment1).contentLength(segment1.size()).build();
System.out.println(blobStore.putBlob("container", seg1Blob));
ByteSource segment2 = ByteSource.wrap("bar".getBytes(Charsets.UTF_8));
Blob seg2Blob = blobStore.blobBuilder("/foo/bar/2").payload(segment2).contentLength(segment2.size()).build();
System.out.println(blobStore.putBlob("container", seg2Blob));
ByteSource manifest = ByteSource.wrap("".getBytes(Charsets.UTF_8));
// TODO: set manifest header here
Blob manifestBlob = blobStore.blobBuilder("/foo/bar").payload(manifest).contentLength(manifest.size()).build();
System.out.println(blobStore.putBlob("container", manifestBlob));
Blob dloBlob = blobStore.getBlob("container", "/foo/bar");
InputStream input = dloBlob.getPayload().openStream();
while (true) {
int i = input.read();
if (i < 0) {
break;
}
System.out.print((char) i); // should print "foobar"
}
}
“待办事项”部分是我的问题。
编辑:
我已经指出,Jclouds自动处理大文件上传,这在我们的案例中不是那么有用。 实际上,我们开始上传第一个片段时,我们不知道文件的大小或下一个片段何时到达。 我们的api旨在使客户端能够按自己选择的大小和自己选择的时间按块大小上传文件,完成后,调用“提交”将这些块制作为文件。 因此,这使我们希望在此处自行上传清单。
根据@Everett Toews的回答,我的代码正确运行了:
public static void main(String[] args) throws IOException {
CommonSwiftClient swift = ContextBuilder.newBuilder("swift").endpoint("http://localhost:8080/auth/v1.0")
.credentials("test:test", "test").buildApi(CommonSwiftClient.class);
SwiftObject segment1 = swift.newSwiftObject();
segment1.getInfo().setName("foo/bar/1");
segment1.setPayload("foo");
swift.putObject("container", segment1);
SwiftObject segment2 = swift.newSwiftObject();
segment2.getInfo().setName("foo/bar/2");
segment2.setPayload("bar");
swift.putObject("container", segment2);
swift.putObjectManifest("container", "foo/bar2");
SwiftObject dlo = swift.getObject("container", "foo/bar", GetOptions.NONE);
InputStream input = dlo.getPayload().openStream();
while (true) {
int i = input.read();
if (i < 0) {
break;
}
System.out.print((char) i);
}
}
jclouds会为您编写清单。 以下是几个可能对您有帮助的示例, UploadLargeObject和largeblob.MainApp 。
尝试使用
Map<String, String> manifestMetadata = ImmutableMap.of(
"X-Object-Manifest", "<container>/<prefix>");
BlobBuilder.userMetadata(manifestMetadata)
如果这样不起作用,则可能必须像CrossOriginResourceSharingContainer.java中那样使用CommonSwiftClient 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.