RAILS S3显示存储在亚马逊S3的pdf文件
答
上传文件到S3时文件名必须有没有空格或特殊字符。
要与空间使用以下
yourmodel.rb
class Video < ActiveRecord::Base
has_attached_file :video,
:path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name",
:url => "/system/:attachment/:id/:style/:normalized_video_file_name"
Paperclip.interpolates :normalized_video_file_name do |attachment, style|
attachment.instance.normalized_video_file_name
end
def normalized_video_file_name
"#{self.id}-#{self.video_file_name.gsub(/[^a-zA-Z0-9_\.]/, '_')}"
end
end
什么是我们在这里做上传文件?很简单,在has_attached_file中,我们编辑paperclip默认返回路径和url的方式,这是保存和加载文件以显示它时最相关的组件。回形针默认值是:前面有
path default => ":rails_root/public/system/:attachment/:id/:style/:filename"
url default => "/system/:attachment/:id/:style/:filename"
价值观 ':' 是标准的插值回形针有
http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
答
你需要一个补充:s3_headers进入您的has_attachment行:
has_attached_file :asset,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "uploads/:id/:basename.:extension",
:s3_headers => {"Content-Disposition" => "attachment"},
:s3_permissions => 'authenticated-read',
:s3_protocol => "http",
:bucket => "my_bucket_or_something"