Search code examples
ruby-on-railsherokuamazon-s3uploadfog

Uploading with fog to S3 works from localhost but not with heroku


Using carrierwave and fog gems to upload images to S3; it works just fine when I do it from localhost:3000. But after deploying to heroku, when I try to upload an image I get the "something went wrong" screen. If I create the same post without trying to upload an image, everything else posts just fine.

Again, in local, when I upload the image it goes straight to my bucket in S3. Why doesn't the same thing happen when I perform the same action from heroku??

I've done all the migrations and pushes, I even tried adding the fog-aws gem (even though aws isn't the problem, heroku is). Please help! :(

products_controller.rb

  def update
    @product.category_id = params[:category_id]

    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

Here is the heroku log, with just the errors.

2017-02-10T06:21:48.471592+00:00 app[web.1]: I, [2017-02-10T06:21:48.471528 #4]  INFO -- : [4cc29852-5ad1-446c-8589-4d67bb97f2cc] Completed 500 Internal Server Error in 614ms (ActiveRecord: 9.3ms)
2017-02-10T06:21:48.472558+00:00 app[web.1]: F, [2017-02-10T06:21:48.472497 #4] FATAL -- : [4cc29852-5ad1-446c-8589-4d67bb97f2cc] Excon::Error::Forbidden (Expected(200) <=> Actual(403 Forbidden)
2017-02-10T06:21:48.472560+00:00 app[web.1]: excon.error.response
2017-02-10T06:21:48.472562+00:00 app[web.1]:   :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AllAccessDisabled</Code><Message>All access to this object has been disabled</Message><RequestId>EB063C36E37005DB</RequestId><HostId>6J4nl+P2RoUKEg1D23Fb3dxokR0I5piyBK3/3wYVRmyGzyFxzFseVmTUoZ6GpMFMUiEVQSH2GVA=</HostId></Error>"

Solution

  • I didn't have this line in my carrierwave.rb (where the fog credentials are), although I don't think it was necessary:

    config.cache_dir = "#{Rails.root}/tmp/uploads"

    The ultimate problem was that I never ran a crucial figaro command in the terminal to inform heroku of my AWS credentials. I had done that individually some other way, but apparently that wasn't good enough. All I needed to do was enter this:

    $ figaro heroku:set -e production