2014-04-04 40 views
2

我正在使用Apache JClouds連接到我的Openstack Swift安裝。我設法從Swift上傳和下載對象。但是,我沒有看到如何將動態大對象上傳到Swift。如何在Apache JClouds中設置HTTP標頭?

要上傳動態大對象,我需要首先上傳所有段,這可以照常進行。然後我需要上傳一個清單對象來合併它們。問題是告訴Swift這是一個明顯的對象,我需要設置一個特殊的頭文件,我不知道如何使用JClouds API來做到這一點。

這是來自openstack官方網站的一個動態大型對象example

我正在使用的代碼:

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旨在使客戶端能夠以他們自己選擇的大小和自己選擇的時間塊上傳文件,並在完成時調用「提交」以將這些塊作爲文件。所以這讓我們想要在這裏上傳清單。

回答

2

根據@Everett託斯的回答,我有我的代碼運行正常:

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); 
    } 
} 
1

jclouds爲您編寫清單。這裏有幾個例子可以幫助你,UploadLargeObjectlargeblob.MainApp

+0

我已經注意到了這些片段,但是對我們來說不是非常有幫助。看到上面編輯的問題,對不起,我的原產地問題不清楚。但是,謝謝,你們真棒! – fengye87

0

使用

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

如果不工作,你可能有CrossOriginResourceSharingContainer.java使用CommonSwiftClient喜歡嘗試。

+0

嘗試過用戶元數據但沒有運氣,這在http頭中沒有任何影響。但是CommonSwiftClient完美工作,並且我在下面發佈了我的固定代碼。非常感謝! – fengye87

相關問題