例:
require 'sinatra' get '/somekey' do headers('Content-Type' => "image/jpeg") ["http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg", "http://img.brothersoft.com/screenshots/softimage/j/jpeg_converter-4567-1.jpeg"].sample end
我想回复一个未托管在我的服务器上的图像.
我怎么能得到这个?
注意:图像的链接不是秘密的(因为它托管在S3上).它是为一个生成identicons的网站.
结帐http://identico.in/[insert_any_key_here].原因是我希望服务器进行查找,如果图像已存在于S3上,则使用该图像,如果不存在,则生成一个然后将其上载到s3.
注意:如果我这样做:
require "open-uri" open ["http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg", "http://img.brothersoft.com/screenshots/softimage/j/jpeg_converter-4567-1.jpeg"].sample
然后它工作,但是,我觉得这可能会慢很多,因为我的服务器首先必须下载图像并打开它,然后用户必须从我的服务器下载图像.
是的,如果你想从你的服务器发送,你需要在发送之前将它放在你的服务器上.所以大多数时候你需要使用send_file open('link')
并成为存储服务器和客户端的代理.
require 'sinatra' require 'open-uri' get '/' do send_file open('http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg'), type: 'image/jpeg', disposition: 'inline' end
但是如果链接不是秘密,你可以只渲染一些javascript,它将在浏览器中打开图像.
require 'sinatra' get '/' do "<script>document.location = 'http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg'</script> end