Using Send_File to a Remote Source (Ruby on Rails)

后端 未结 3 595
再見小時候
再見小時候 2020-12-08 08:37

In my app, I have a requirement that is stumping me.

I have a file stored in S3, and when a user clicks on a link in my app, I log in the DB they\'ve clicked the lin

3条回答
  •  囚心锁ツ
    2020-12-08 09:19

    You can read the file from S3 and write it locally to a non-public directory, then use X-Sendfile (apache) or X-Accel-Redirect (nginx) to serve the content.

    For nginx you would include something like the following in your config:

    
                location /private {
                                     internal;
                                     alias /path/to/private/directory/;
                }
    

    Then in your rails controller, you do the following:

    
       response.headers['Content-Type'] = your_content_type
       response.headers['Content-Disposition'] = "attachment; filename=#{your_file_name}"
       response.headers['Cache-Control'] =  "private"
       response.headers['X-Accel-Redirect'] = path_to_your_file
       render :nothing=>true
    

    A good writeup of the process is here

提交回复
热议问题