【问题标题】:Rails and Amazon S3Rails 和 Amazon S3
【发布时间】:2013-05-01 05:43:26
【问题描述】:

我正在关注这个tutorial,并使用回形针和 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

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

【问题讨论】:

  • 你的bucket在哪个区域?我认为如果您尝试“通过”不同区域连接,S3 会将您重定向到正确的区域(您的代码将使用的默认区域是 us-east-1)
  • 这修复了从 S3 下载的问题,但现在下载已损坏且无法打开。我收到以下错误:无法打开文件“Adobe-PDF-Document-icon (1).png”。 t 可能已损坏或使用 Preview 无法识别的文件格式。

标签: ruby-on-rails amazon-s3


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助。这是桶区域不匹配。我现在可以从 S3 下载,但打开文件时出现错误:无法打开文件“Adobe-PDF-Document-icon (1).png”。 t 可能已损坏或使用 Preview 无法识别的文件格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2017-03-27
  • 1970-01-01
相关资源
最近更新 更多