簡體   English   中英

RAILS S3顯示存儲在亞馬遜S3中的pdf文件

[英]RAILS S3 displaying pdf file stored in amazon s3

如何在Rails應用程序中顯示存儲在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中,我們編輯回形針默認情況下返回路徑和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

您需要在has_attachment行中添加:s3_headers條目:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM