【问题标题】:Not able to move files from Google Drive to Blobstore无法将文件从 Google Drive 移动到 Blobstore
【发布时间】:2016-02-12 16:58:27
【问题描述】:

我正在使用以下代码将文件从 Google 驱动器移动到 blobstore。但现在 FileWriteChannel 已被弃用,代码无法正常工作。这个问题有替代解决方案吗?

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

【问题讨论】:

    标签: google-app-engine google-drive-api blobstore


    【解决方案1】:

    你需要使用Java Client library:

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

    【讨论】:

    • 如果您打算将完整的内容加载到内存中(希望是小文件),那么您可以使用 createOrReplace 方法来接受内容(因为它会节省您的 RPC 调用)。此外,您应该考虑使用gcloud-java-storage,它在 AE 和 AE 之外都可以使用。
    • 但我需要将文件存储在 Blobstore 而不是谷歌存储桶中。我正在将一万个文件从 Google Drive 迁移到 blobstore。
    • Blobstore 在文档中被列为“被取代的存储解决方案”。这意味着它可以在某个时候停止/弃用。
    猜你喜欢
    • 2018-10-22
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多