我跟着这个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
任何帮助将不胜感激。谢谢!
你的存储桶在哪个区域?我认为如果您尝试通过不同区域(您的代码将使用的默认区域是us-east-1)连接“S3”,S3会将您重定向到正确的区域 – 2013-04-30 21:42:59
这固定了S3下载的问题,但现在下载已损坏并无法打开。我收到以下错误:无法打开文件“Adobe-PDF-Document-icon(1).png”。 t可能会被损坏或使用Preview无法识别的文件格式。 – user2337569 2013-05-01 01:55:26