TK.*_*TK. 5 privacy file-upload ruby-on-rails image
这是一个故事:
我打算使用Paperclip来处理上传.
如果我将图像存储在"RAILS_ROOT/public/images"下,任何猜测文件名称的人都可以访问这些文件.(例如,访问http://example.com/public/images/uploads/john/family.png)
我需要使用img标签显示图像,所以我不能放置文件,除了public.
如何确保其他人无法访问用户或组的图像?
(如果我用Paperclip无法做到这一点,那么什么是好的解决方案?)
您可以使rails服务器输出图像文件的内容.这是通过控制器操作完成的(大多数操作都打印HTML,但是这个会打印JPG,例如).
然后,您可以使用授权系统限制控制器级别的访问权限!
class ImagesController
#Default show Image method streams the file contents.
#File doesn't have to be in public/ dir
def show
send_file @image.filename, :type => @image.content_type,
:disposition => 'inline'
end
# Use your favorite authorization system to restrict access
filter_access_to :show, :require => :view, :attribute_check => :true
end
Run Code Online (Sandbox Code Playgroud)
在HTML代码中,您可以使用:
<img src="/images/show/5" />
Run Code Online (Sandbox Code Playgroud)
我会让 Paperclip 在后端使用 S3,将上传的文件设置为私有,然后使用“查询字符串请求身份验证替代方案”为我的图像标签生成 URL。
http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTAuthentication.html
这是我在类似应用程序中执行此操作的方法。
\n\n我的看起来像这样:
\n\ndef s3_url(style = :original, time_limit = 30.minutes)\n self.attachment.s3.interface.get_link(attachment.s3_bucket.to_s, attachment.path(style), time_limit)\nend\nRun Code Online (Sandbox Code Playgroud)\n\n| 归档时间: |
|
| 查看次数: |
5639 次 |
| 最近记录: |