【问题标题】:Google App Engine getImageServingUrl with download file name带有下载文件名的 Google App Engine getImageServingUrl
【发布时间】:2017-03-06 13:24:46
【问题描述】:

我正在使用 getImageServingUrl 方法生成图像的 URL,它与 List of all the App Engine images service get_serving_url() URI options 中列出的选项一起工作得很好

我可以添加“d”选项来强制下载文件,而不仅仅是在浏览器中显示它。问题是文件名默认为“unnamed.[ext]”(其中 [ext] 是源文件扩展名)。有没有办法可以将原始文件名传递给请求?

我可以使用 CloudStorageTools::serve 以给定名称保存文件,但例如我不能使用图像大小调整功能。

【问题讨论】:

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


    【解决方案1】:

    我不知道任何允许这样做的选项。 但我有另一种方式的概念,这有点复杂,但对你来说可能是一个不错的选择。 您可以使用 nginx 构建反向代理,它将通过您自己的域为您提供谷歌图像。然后您可以将原始请求中的文件名作为参数发送。在 nginx 中,您可以在代理请求之前从请求中删除文件名参数,并将其添加为标头以在响应中定义文件名。 作为 nginx 配置中的一个进程,这非常简单:

    location /image-storage/ {
        expires 30d;
    
        #Get rid of headers to be overwritten
        proxy_hide_header Pragma;
        proxy_hide_header Cache-Control;
        proxy_hide_header Content-Disposition;
    
        #Add caching headers
        add_header Pragma public;
        add_header Cache-Control "public";
        #If the parameter "filename" is sent, use it as filename
        if ($arg_filename) {
            add_header Content-Disposition 'inline; filename="$arg_filename"';
        }
        #If the parameter "dl_filename" is sent, use it as filename and init download (attachment)
        if ($arg_dl_filename) {
            add_header Content-Disposition 'attachment; filename="$arg_dl_filename"';
        }
    
        #Proxy the request to google
        proxy_pass       https://lh3.googleusercontent.com/;
        proxy_set_header Host lh3.googleusercontent.com;
    
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        proxy_buffering off;
    }
    

    这样,https://yourdomain.com/image-storage/longgoogletokenhere?dl_filename=test.jpg 之类的 url 将被代理到 https://lh3.googleusercontent.com/longgoogletokenhere?dl_filename=test.jpg,并且响应将获得用于缓存和文件名的新标头。

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2016-11-26
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多