2013-04-30 214 views
0

我跟着这个tutorial,并建立一个使用回形针和S3的rails文件共享应用程序。上传文件到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 

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 

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

+0

你的存储桶在哪个区域?我认为如果您尝试通过不同区域(您的代码将使用的默认区域是us-east-1)连接“S3”,S3会将您重定向到正确的区域 – 2013-04-30 21:42:59

+0

这固定了S3下载的问题,但现在下载已损坏并无法打开。我收到以下错误:无法打开文件“Adobe-PDF-Document-icon(1).png”。 t可能会被损坏或使用Preview无法识别的文件格式。 – user2337569 2013-05-01 01:55:26

回答

0

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

这可能是S3需要一段时间,直到文件变得可访问

+0

感谢您的帮助。这是桶区域不匹配。我现在可以从S3下载,但打开文件时出现错误:无法打开文件“Adobe-PDF-Document-icon(1).png”。 t可能会被损坏或使用Preview无法识别的文件格式。 – user2337569 2013-05-01 01:56:59

+0

OS X Preview应该能识别.png格式。 – Tilo 2013-05-01 04:34:48