Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save_and_process post processing 403 Forbidden Carrierwave_direct S3 Fog

I'm trying to develop direct fileuploads to S3 for my app. I'm following the github tutorial for this and everything is more or less ok but get an error message when trying to make the post processing.

I did the following:

I have an activerecord model called clip.rb:

class Clip < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
  mount_uploader :avatar, AvatarUploader

  attr_accessible :id, :avatar, :name, :clipat_file_name, :attachable_id, :attachable_type, :clipat, :project_id, :user_id, :path, :parent_id, 

  def save_and_process_avatar(options = {})
    if options[:now] or 1==1
      self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)
      save
    else
      Resque.enqueue(AvatarProcessor, attributes)
    end
  end

Then I have an uploader: avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base
   include CarrierWave::RMagick
   include CarrierWaveDirect::Uploader
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}"  #I removed /#{model.id} from the template because it creates an empty directory on the server. But If I put it back, the same problem remains
  end
  version :thumb do
     process :resize_to_limit => [50, 50]
   end
end

and an avatar controller:

class AvatarsController < ApplicationController
  def new
    @uploader = Clip.new.avatar
    @uploader.success_action_redirect = 'http://localhost:3000/clips'
  end
end

and finally my clip_controller:

class ClipsController < ApplicationController
  def index
    if params[:key]
      key=params[:key].split("/")
      clip = Clip.new
      clip.attachable_id = key[3]
      clip.attachable_type = "Pmdocument"
      clip.key = params[:key]
#      clip.save
      clip.save_and_process_avatar
    end
    @clips = Clip.where("avatar is not null")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @clips.collect { |p| p.to_jq_upload }.to_json }
    end
  end

When I upload a file, if I just save my "clip", everything is ok. If I use the save_and_process method however, an error arises at line: self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)

This is the error message:

OpenURI::HTTPError (403 Forbidden):
  app/models/clip.rb:38:in `save_and_process_avatar'
  app/controllers/clips_controller.rb:22:in `index'

Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.2ms)

I've been hanging on this for two days so, any help would be greatly appreciated!!! Thanks!!! Nicolas.

like image 430
ndemoreau Avatar asked Nov 04 '22 06:11

ndemoreau


1 Answers

My bet is that the URL supplied to self.remote_avatar_url is incorrect. I had this same problem and the code that CWDirect gem provides didn't work for me and gave me an incorrect URL, thus CarrierWave couldn't download and process the image. The whole 403 Forbidden error message was a crap message from Amazon--this leads one to believe that there is something wrong with Permissions; in my case there was absolutely nothing wrong with permissions. It was just that there was no image there. Here is the code that got this working for me, notice I've changed how the URL is formed:

def save_and_process_image(options = {})
if options[:now]
  # debugger
  self.remote_image_url = image.direct_fog_url+self.key # OLD CODE THAT AINT WORKIN! --> image.direct_fog_url(:with_path => true)
  save
else
  # Resque.enqueue(AvatarProcessor, attributes)
  # TODO: Implement background processing
end
end

Note that the name of my mounted field is image and not avatar.

How I got to this point and fixed it--try this out, use the rails debugger (just uncomment the debugger line above) to freeze the program just before the self.remote_image_url line, then while in debug mode type irb to start up the console. Then you can print out and see really what value 'image.direct_fog_url(:with_path => true)' is giving you. You can copy and paste this into a browser. If it's wrong (probably is) then you will get the silly Permissions error (even though it's not a permissions problem), but when it's correct either you will see the uploaded image or the image will download.

It's a good idea to have your Amazon S3 console open and viewing your dev bucket so you can find the image that just uploaded. Find the image in the console and go to its properties and you can see the web address/url that you are supposed to be using.

Hope this helps. Because of the misleading error this was hard to track down for me, I spent a bunch of time trying to correct permissions on my S3 bucket but this wasn't the problem, just that code from the CWDirect github page doesn't work for me (gem version??).

like image 183
FireDragon Avatar answered Nov 26 '22 01:11

FireDragon