Google App Engine getImageServingUrl,下载文件名 [英] Google App Engine getImageServingUrl with download file name

查看:119
本文介绍了Google App Engine getImageServingUrl,下载文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 getImageServingUrl 方法来生成图像的URL,它与以下列出的选项一起工作良好:

I'm using getImageServingUrl method to generate URL to an image and it works nicely along with options listed at List of all the App Engine images service get_serving_url() URI options

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

I can add "d" option to force file download instead of just presenting it in the browser. The thing is that file name defaults to "unnamed.[ext]" (where [ext] is source file extension). Is there a way I can pass the original filename somewhere to the request?

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

I can use CloudStorageTools::serve to save file under given name but for example I can't use the image resizing features then.

推荐答案

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

I am not aware of any option that would allow this. But I have a concept for another way, that is a bit complicated, but might be a good option for you. You can use nginx to build a reverse proxy, that will serve the google images for you through your own domain. Then you could send the filename in your original request as a parameter. In nginx you can get rid of the filename parameter from the request before proxying the request and add it as a header to define the filename in the response. This is pretty straighforward as a process in your nginx config:

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 将代理到< a href =https://lh3.googleusercontent.com/longgoogletokenhere?dl_filename=test.jpg =nofollow noreferrer> https://lh3.googleusercontent.com/longgoogletokenhere?dl_filename=test.jpg 并且响应会为缓存和文件名获得新的标头。

This way a url like: https://yourdomain.com/image-storage/longgoogletokenhere?dl_filename=test.jpg would be proxied to https://lh3.googleusercontent.com/longgoogletokenhere?dl_filename=test.jpg and the response would get new headers for caching and filename.

这篇关于Google App Engine getImageServingUrl,下载文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆