2016-04-11 223 views
1

我用下面的代碼:谷歌雲存儲 - java的

GcsService gcsService = GcsServiceFactory.createGcsService(); 
    GcsFilename filename = new GcsFilename(BUCKETNAME, fileName); 
    GcsFileOptions options = new GcsFileOptions.Builder() 
     .mimeType(contentType) 
     .acl("public-read") 
     .addUserMetadata("myfield1", "my field value") 
     .build(); 
    @SuppressWarnings("resource") 
    GcsOutputChannel outputChannel = 
     gcsService.createOrReplace(filename, options); 
    outputChannel.write(ByteBuffer.wrap(byteArray)); 
    outputChannel.close(); 

的問題是,當我嘗試存儲的視頻文件,我必須將文件存儲在其中的ByteArray可能會導致內存問題。

但我找不到任何接口來做同樣的流。

問題:

  1. 我應該擔心在AppEngine上SRV MEM的問題,或者是他們能夠保持1分鐘的視頻在MEM的?

  2. 是否可以使用流代替字節數組?怎麼樣?

  3. 我正在讀取的字節數爲byte[] byteArray = IOUtils.toByteArray(stream);我應該使用字節數組作爲真正的緩衝區,只是讀取塊並將它們上傳到GCS?我怎麼做?

回答

0

可用內存量取決於您配置的appengine實例類型。如果可以的話,將這些數據流式傳輸似乎是個好主意。

不能確定GcsService API,但是看起來你可以使用gcloud存儲API做到這一點: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java

此代碼可能工作(未經測試)...

final BlobInfo info = BlobInfo.builder(bucket.getBucketName(), "name").contentType("image/png").build(); 

final ReadableByteChannel src = Channels.newChannel(stream); 
final WriteChannel dst = gcsStorage.writer(info); 

fastChannelCopy(src, dst); 

private void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { 
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); 
    while (src.read(buffer) != -1) { 
     buffer.flip();  // prepare the buffer to be drained 
     dest.write(buffer); // write to the channel, may block 
     // If partial transfer, shift remainder down 
     // If buffer is empty, same as doing clear() 
     buffer.compact(); 
    } 
    // EOF will leave buffer in fill state 
    buffer.flip(); 

    // make sure the buffer is fully drained. 
    while (buffer.hasRemaining()) { 
     dest.write(buffer); 
    } 
} 
+0

注意,BLOB API不再被支持 - 我不認爲這個代碼會起作用。 –

+1

這不是blob api。僅僅因爲你看到一個名爲BlobInfo的類並不意味着這是不受支持的blob api。我目前正在使用這個API來獲得大量的流量。請看這裏,並重新考慮您的downvote:http://googlecloudplatform.github.io/gcloud-java/0.2.0/index.html – depsypher