本文介绍了Rails和亚马逊S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与这个教程,建设轨道文件中使用曲别针和S3共享应用程序。该文件上传到S3的工作完美,但是当我点击上传的文件名,我得到这个错误:

I'm following along with this tutorial and building a rails file sharing app using paperclip and S3. The file upload to S3 is working perfectly but when I click on the uploaded file name I get this error:

OpenURI::HTTPError in AssetsController#get

301 Moved Permanently (Invalid Location URI)

它指向我

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

下面是我的code:

assets.controller.rb

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

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

任何帮助将是很大的AP preciated。谢谢!

Any help would be greatly appreciated. Thanks!

推荐答案

做同样的事情发生,如果你上传后,经过几分钟的尝试?

does the same thing happen if you try after a couple of minutes after uploading?

这可能是因为S3需要一段时间,直到文件将成为访问

It could be that S3 takes a while until the file becomes accessible

这篇关于Rails和亚马逊S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:18