2016-02-12 138 views
0

我使用下面的代碼將文件從Google雲端硬盤移動到blobstore。但現在FileWriteChannel已被棄用,代碼無法正常工作。有沒有其他解決方案來解決這個問題?無法將文件從Google雲端硬盤移動到Blobstore

private BlobKey getBlobKey(File f, DriveObject driveObject) 
     throws IOException, MalformedURLException { 

    Drive service = ((GoogleDrive) driveObject).getService(); 

    byte[] buffer = new byte[(int) f.getFileSize().intValue()]; 
    GenericUrl url = new GenericUrl(f.getDownloadUrl()); 
    HttpResponse response = service.getRequestFactory() 
       .buildGetRequest(url).execute(); 

    InputStream is = response.getContent(); 

    FileService fileService = FileServiceFactory.getFileService(); 
    AppEngineFile file = null; 
    boolean lock = true; 
    try { 
     file = fileService.createNewBlobFile("application/zip"); 
     FileWriteChannel writeChannel = fileService.openWriteChannel(
        file, lock); 

     int len; 
     while ((len = is.read(buffer)) >= 0) { 
     ByteBuffer bb = ByteBuffer.wrap(buffer, 0, len); 
     writeChannel.write(bb); 
     } 
     writeChannel.closeFinally(); 

    } catch (IOException e) { 
      // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     BlobKey bk = fileService.getBlobKey(file); 

    return bk; 
} 

回答

0

您需要使用Java Client library

GcsOutputChannel outputChannel = 
    gcsService.createOrReplace(fileName, GcsFileOptions.getDefaultInstance()); 
outputChannel.write(ByteBuffer.wrap(content)); 
outputChannel.close(); 
+0

如果您計劃加載完整的內容到內存(希望小文件),那麼你可以使用接受的內容(因爲這將節省createOrReplace方法你的RPC調用)。另外,您應該考慮使用在AE以及AE外部工作的[gcloud-java-storage](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-storage)。 – ozarov

+0

但我需要將文件存儲在Blobstore而不是谷歌存儲桶中。我正在將一萬個文件從Google Drive遷移到Blobstore。 – user3223016

+0

Blobstore在文檔中被列爲「被取代的存儲解決方案」。這意味着它可以在某些時候停用/棄用。 –

相關問題