【问题标题】:Images are always served with name "unnamed.jpg" in Google App Engine在 Google App Engine 中,图像始终以名称“unnamed.jpg”提供
【发布时间】:2018-08-08 00:20:54
【问题描述】:

我正在 Google App Engine 标准环境中开发 PHP 应用程序。

我将图像上传到应用的关联存储桶,然后使用此代码提供图像:

use \google\appengine\api\cloud_storage\CloudStorageTools;

// ...

$path = "gs://my-app.appspot.com/the_image_name.jpg"
$imageUrl = CloudStorageTools::getImageServingUrl($path, ['secure_url' => true, 'size' => 0]);
header("Location: $imageUrl");

当然会有这样的页面重定向:

https://lh3.googleusercontent.com/<some_hash>

图像显示正确,但在浏览器标题中显示“unnamed.jpg”,当我尝试将图像保存到我的计算机时,默认名称显示为“unnamed.jpg”。每次我上传一张图片时都会发生这种情况,无论它的实际名称是什么,或者我是否使用 API 通过我的应用上传它,或者我是否通过网络云控制台直接将它上传到存储桶。

有什么方法可以指定在提供图像时使用什么名称?

【问题讨论】:

    标签: php google-app-engine google-cloud-storage


    【解决方案1】:

    您是否设置了标头:“Content-disposition”、“Content-Transfer-Encoding”和“Content-Type”

    【讨论】:

    • Google 创建了一个用于提供图像的 URL,我必须将我的应用程序的用户重定向到该 Google 提供的 URL。我无法直接设置该响应的标头(因为它是由 Google 处理的)。
    • 在python中我可以访问blobstore_handlers.BlobstoreDownloadHandlercloud.google.com/appengine/docs/standard/python/tools/webapp/…,在那里我可以将GCS密钥转换为blob密钥,App引擎将在没有重定向的情况下提供服务。如果我浏览 BlobstoreDownloadHandler.send_blob 的源代码,它会设置特殊的 appengine 标头“X-AppEngine-BlobKey”,然后 app 引擎必须有一些特殊的代码来检测这一点,然后将 GCS 文件注入到响应负载中。也许你可以在 python 中模拟这个
    【解决方案2】:

    CloudStorageTools::getImageServingUrl 使用 googleusercontent.com 这是一个“沙盒”,因此您无法更改所服务文件的名称。

    我建议您查看CloudStorageTools::serve 方法,您将能够使用save_as 参数设置文件名。请注意,该类型是字符串,而不是文档中提到的布尔值,serve 将花费一些应用引擎计算时间。

    代码应如下所示:

    use \google\appengine\api\cloud_storage\CloudStorageTools;
    
    // ...
    
    $file_name = "gs://my-app.appspot.com/the_image_name.jpg"
    $options = ['save_as' => 'the_image_name.jpg'];
    CloudStorageTools::serve($file_name, $options);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 2021-12-01
      • 2019-08-04
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      相关资源
      最近更新 更多