【问题标题】:BlobstoreService serve() doesn't provide Content-LengthBlobstoreService serve() 不提供 Content-Length
【发布时间】:2014-06-05 09:34:11
【问题描述】:

我想通过 Google App Engine (GAE) 将存储在 Google Cloud Storage (GCS) 上的文件提供给用户。我正在使用以下代码:

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/myBucket/myObjectName");
blobstoreService.serve(blobKey, response);

我不想公开直接的公共 URL,因为文件是私有的并且只能通过 GAE 应用程序访问。用户只有在登录后才能下载。

但是,Content-Length 已从响应中删除(因此 Chrome 中没有进度条),即使我这样做了

response.addHeader("Content-Length", String.valueOf(gcsObject.getSizeBytes()));

response.setContentLength((int) gcsObject.getSizeBytes());

在实际提供文件之前(第一个代码块)。

我从服务器得到的响应如下:

access-control-allow-origin:*
cache-control:private
content-disposition:attachment; filename="MyVideoFile.mp4"
content-type:video/mp4
date:Mon, 21 Apr 2014 10:06:03 GMT
expires:Thu, 01 Jan 1970 00:00:00 GMT
server:Google Frontend
status:200 OK
version:HTTP/1.1

当没有Content-Length Chrome (Version 34.0.1847.116 m) 时,有时会损坏文件 - 例如。如果文件是 180MB,有时是 178MB,有时是 179MB 等等。我不知道这两者是否相关。

我提供的所有文件都在 100-250MB 的文件大小范围内。

如果我将文件公开并通过直接 URL(通过 https://storage.googleapis.com/...)提供它,我会得到 Content-Length 和实际进度条(在 Chrome 中),一切正常(没有损坏)。

解决办法是什么?

【问题讨论】:

    标签: java google-app-engine google-chrome google-cloud-storage blobstore


    【解决方案1】:

    这是一个已知的issue

    如果这是一个公共文件,那么正如您已经提到的,您可以通过直接 Cloud Storage url 提供它。

    但是,如果这是一个私有文件,那么您可以使用GCS Client Library 来读取 blob 内容并通过您的 servlet 提供它。然后Content-Length header 将由 GAE 自动设置。

    【讨论】:

    • 如果我使用 GCS 客户端库并直接提供服务(使用 copy(Channels.newInputStream(readChannel), response.getOutputStream()); 复制字节数组以响应)我在 App Engine 上收到 32MB HTTP 限制错误。对于大于 32MB 的文件,是否有一些样板/模板 servlet/代码,使用 multipart 下载或...?
    • AFAIK,没有。这是对 GAE 前端实例的硬性限制。
    • 在这种情况下,您应该使用直接云存储 URL 并查看 ACL 以进行非公共访问:developers.google.com/storage/docs/accesscontrol
    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多