Serve HTML files stored on S3 on a Rack app

痴心易碎 提交于 2019-12-13 19:04:09

问题


Say I have some HTML documents stored on S3 likes this:

  • http://alan.aws-s3-bla-bla.com/posts/1.html
  • http://alan.aws-s3-bla-bla.com/posts/2.html
  • http://alan.aws-s3-bla-bla.com/posts/3.html
  • http://alan.aws-s3-bla-bla.com/posts/1/comments/1.html
  • http://alan.aws-s3-bla-bla.com/posts/1/comments/2.html
  • http://alan.aws-s3-bla-bla.com/posts/1/comments/3.html
  • etc, etc

I'd like to serve these with a Rack (preferably Sinatra) application, mapping the following routes:

get "/posts/:id" do
 render "http://alan.aws-s3-bla-bla.com/posts/#{params[:id]}.html"
end

get "/posts/:posts_id/comments/:comments_id" do
 render "http://alan.aws-s3-bla-bla.com/posts/#{params[:posts_id]}/comments/#{params[:comments_id}.html"
end

Is this a good idea? How would I do it?


回答1:


There would obviously be a wait while you grabbed the file, so you could cache it or set etags etc to help with that. I suppose it depends on how long you want to wait and how often it is accessed, its size etc as to whether it's worth storing the HTML locally or remotely. Only you can work that bit out.

If the last expression in the block is a string that will automatically be rendered, so there's no need to call render as long as you've opened the file as a string.

Here's how to grab an external file and put it into a tempfile:

require 'faraday'
require 'faraday_middleware'
#require 'faraday/adapter/typhoeus' # see https://github.com/typhoeus/typhoeus/issues/226#issuecomment-9919517 if you get a problem with the requiring
require 'typhoeus/adapters/faraday'

configure do
  Faraday.default_connection = Faraday::Connection.new( 
    :headers => { :accept =>  'text/plain', # maybe this is wrong
    :user_agent => "Sinatra via Faraday"}
  ) do |conn|
    conn.use Faraday::Adapter::Typhoeus
  end
end

helpers do
  def grab_external_html( url )
    response = Faraday.get url # you'll need to supply this variable somehow, your choice
    filename = url # perhaps change this a bit
    tempfile = Tempfile.open(filename, 'wb') { |fp| fp.write(response.body) }
  end
end

get "/posts/:whatever/" do
  tempfile = grab_external_html whatever # surely you'd do a bit more here…
  tempfile.read
end

This might work. You may also want to think about closing that tempfile, but the garbage collector and the OS should take care of it.



来源:https://stackoverflow.com/questions/14295178/serve-html-files-stored-on-s3-on-a-rack-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!