0

我正在学习本教程并使用回形针和 S3 构建一个 Rails 文件共享应用程序。文件上传到 S3 工作正常,但是当我单击上传的文件名时,我收到此错误:

OpenURI::HTTPError in AssetsController#get

301 Moved Permanently (Invalid Location URI)

它指向我

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

这是我的代码:

资产.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 

资产.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

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

4

1 回答 1

0

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

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

于 2013-04-30T21:43:26.593 回答