2011-08-31 87 views
10

我使用:的Rails 3 - 亞馬遜S3回形針歐盟問題

Paperclip 2.3.16 
Rails 3.0.9 
Ruby 1.9.2 
AWS - S3 0.6.2 

我試圖用回形針上傳到歐盟(愛爾蘭)的桶。我已經在我的模型如下:

has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :original => '400x548'}, 
      :storage => :s3, 
         :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
         :url => 'flyers/:id/:style/:basename.:extension', 
         :path => 'flyers/:id/:style/:basename.:extension', 
         :bucket => 'fsight' 

在我的environment.rb我已通過使用AWS/S3默認主機的有關歐盟一個寫:

require "aws/s3" 
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com" 

該作品很好,它允許我上傳圖像,並且我可以使用AWS管理控制檯驗證圖像上傳/刪除。

但是,我試圖在我的網站上顯示圖像時遇到問題。圖像無法加載,我已經確定了原因,因爲生成的URL使用舊的默認主機。例如:

應然: https://s3-eu-west-1.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg

它實際上是什麼:http://s3.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg?1314801178

正如你所看到的,它使用了舊的默認主機。

我試圖配售:

Paperclip.interpolates(:s3_eu_url) do |att, style| 
    "#{att.s3_protocol}://s3-eu-west-1.amazonaws.com/#{att.bucket_name}/#{att.path(style)}" 
end 

但隨後開始收到以下錯誤:

wrong number of arguments (0 for 1) 

Extracted source (around line #9): 

<img src= <%= @event.image.url(:original) %> 

我知道回形針具有使用歐盟桶一些問題,但可能有人幫助我嗎?

回答

8

你試過這個解決方法嗎?

Paperclip et les European S3 buckets

甚至這一個?

Paperclip, S3, and European Buckets

+0

我試了第二個,就像你在問題中看到的一樣。我不知道第一個仍然有效,我現在就試一試。 感謝您的指針:)我會讓你知道它是怎麼回事。 – Ammar

+1

嗨,如上所述,我試過第二個,現在我已經嘗試了第一個,都無濟於事。 我可以讓上傳工作,但出於某種原因,生成的從S3存儲區獲取圖像的URL總是錯誤的,正如問題所述,有幫助嗎? – Ammar

1
這裏

同樣的問題,只是解決了通過下列選項has_attached_file

:url => ':s3_domain_url' 

欲瞭解更多信息請參見這裏http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3

Normally, this won't matter in the slightest and you can leave the default (which is path-style, or :s3_path_url). But in some cases paths don't work and you need to use the domain-style (:s3_domain_url).

30

你不需要解決歐盟問題。

回形針中默認的aws-s3存儲後端是replaced by the AWS SDK for Ruby,這也是AWS使用AWS時推薦的方式。

只需插入

gem 'aws-sdk' 

到您的Gemfile和運行bundle install

如果你想要像https://s3-eu-west-1.amazonaws.com/some_path_goes_here,嘗試配置模型的has_attached_file有下列選項

:storage => :s3, 
:s3_credentials => "#{Rails.root}/config/s3.yml", 
:s3_permissions => :private, 
:s3_protocol => 'https', 
:s3_host_name => 's3-eu-west-1.amazonaws.com', 
:path => ":filename" 

如果你不想使用https可以去除:s3_protocol,如果你想改變區域,選項:s3_host_name是正確的選擇。你也可以把它放到配置文件中。

希望這會有所幫助。

+0

感謝@toashd這真的幫了我。還要注意,s3主機可以在s3.yml文件中指定爲s3_host_name:'s3-eu-west-1.amazonaws.com'。我還必須指定's3_endpoint:'s3-eu-west-1.amazonaws.com''來獲取遷移本地文件的rake任務。 – Leo

5

我加

Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com' 

在初始化文件夾paperclip.rb,它爲我工作得很好。