2

我正在使用 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"
    }
}

“TODO”部分是我的问题。


编辑:

有人指出,Jclouds 会自动处理大文件上传,这在我们的案例中不是很有用。事实上,在我们开始上传第一个片段时,我们不知道文件有多大,也不知道下一个片段何时到达。我们的 api 旨在使客户端能够以自己选择的大小和自己选择的时间上传他们的文件,完成后,调用“提交”将这些块制作为文件。所以这让我们想在这里自己上传清单。

4

3 回答 3

2

根据@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);
    }
}
于 2014-04-09T02:29:53.463 回答
1

jclouds 为您编写清单。下面是几个可能对您有所帮助的示例,UploadLargeObjectlargeblob.MainApp

于 2014-04-04T14:44:49.100 回答
0

尝试使用

Map<String, String> manifestMetadata = ImmutableMap.of(
    "X-Object-Manifest", "<container>/<prefix>");
BlobBuilder.userMetadata(manifestMetadata)

如果这不起作用,您可能必须像 CrossOriginResourceSharingContainer.java 那样使用CommonSwiftClient

于 2014-04-08T23:14:42.883 回答