【问题标题】:How to read large files from google drive into gae blobstore如何将谷歌驱动器中的大文件读入 gae blobstore
【发布时间】:2013-05-11 09:48:41
【问题描述】:

这是我目前用于将文件从谷歌驱动器读取到谷歌应用引擎的 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;
    }

这适用于小文件,但如果文件超过 ~25MB,则会出错。有没有更好的方法来做我想做的事情?

这是我看到的错误

Uncaught exception from servlet
com.google.appengine.api.urlfetch.ResponseTooLargeException: The response from url https://doc-0o-bs-docs.googleusercontent.com/docs/securesc/97o44sjeam7f23b1flie9m3qqidhrago/97q4akk8bmpub24itqhclo1vakjusu84/1368259200000/14350098165097390874/14350098165097390874/0ByS2XCxCEzq-NUVuajAwWGVoR1U?h=16653014193614665626&e=download&gd=true was too large.
    at com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:132)
    at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:43)
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.fetchResponse(URLFetchServiceStreamHandler.java:417)
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getInputStream(URLFetchServiceStreamHandler.java:296)
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getResponseCode(URLFetchServiceStreamHandler.java:149)
    at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:37)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:95)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:980)

我应该补充一下

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

返回

new Drive.Builder(httpTransport, jsonFactory, cred)
            .build()

【问题讨论】:

  • App Engine 限制 HTTP 消息正文的大小。
  • 我意识到这是怎么回事,但有没有办法解决这个问题?我可以将大文件读入 blobstore,这一点非常重要。也许我需要在多条消息中阅读它。
  • 我可以通过 blobstoreservice 从我的计算机上传我需要的文件,但这还不够好,因为我需要能够通过驱动器接口来完成。

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


【解决方案1】:

您可以从云端硬盘分块下载文件。在您的下载请求中使用Range 标头,如Google Drive documentation 中所述。

Range: bytes=500-999

【讨论】:

【解决方案2】:

已弃用此 FilsService 和 FileWriteChannel api,取而代之的是 App Engine GCS 客户端。

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2021-06-13
    • 2020-11-28
    • 2011-12-25
    相关资源
    最近更新 更多