Rails和Amazon S3

我正在关注本教程并使用paperclip和S3构建rails文件共享应用程序。 上传到S3的文件工作正常,但是当我点击上传的文件名时,我收到此错误:

OpenURI::HTTPError in AssetsController#get 301 Moved Permanently (Invalid Location URI) 

它指向我

 app/controllers/assets_controller.rb:15:in `get' 

这是我的代码:

assets.controller.rb

 def get asset = current_user.assets.find_by_id(params[:id]) if asset #Parse the URL for special characters first before downloading data = open(URI.parse(URI.encode(asset.uploaded_file.url))) #then again, use the "send_data" method to send the above binary "data" as file. send_data data, :filename => asset.uploaded_file_file_name #redirect to amazon S3 url which will let the user download the file automatically #redirect_to asset.uploaded_file.url, :type => asset.uploaded_file_content_type else flash[:error] = "Don't be cheeky! Mind your own assets!" redirect_to root_url end end 

assets.controller.rb

 attr_accessible :user_id, :uploaded_file belongs_to :user #set up "uploaded_file" field as attached_file (using Paperclip) has_attached_file :uploaded_file, :path => "assets/:id/:basename.:extension", :storage => :s3, :s3_credentials => ::Rails.root.join('config/amazon_s3.yml'), :bucket => "Sharebox" validates_attachment_size :uploaded_file, :less_than => 10.megabytes validates_attachment_presence :uploaded_file def file_name uploaded_file_file_name end end 

任何帮助将不胜感激。 谢谢!

如果您在上传后几分钟后尝试,会发生同样的事情吗?

可能是S3需要一段时间才能访问该文件