简体   繁体   中英

Rails and Amazon 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)

It's pointing me to

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

Here is my code:

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

Any help would be greatly appreciated. Thanks!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM