Search code examples
ruby-on-railsrubyamazon-s3carrierwavefog

Carrierwave - Save to S3 as an independent action


My users store an external image URL (http://their-site.com/photo.jpg) in - for example - @user.external_image. I'm trying to write a method for the User class that takes that URL and saves it to S3 using Carrierwave.

So on the above @user, I'd like to run @user.save_to_s3 and have it "upload" the image to S3. I've tried to do this by mounting an uploader on :s3_image to the User class and writing the following method:

def save_to_s3
  self.remote_s3_image_url = self.external_image
  save
end

But I get the following error when I call that method on a @user record:

"ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key"

So it's getting close, but it's not retrieving my S3 credentials - even though they're set. I'd appreciate any thoughts or suggestions.


Solution

  • The problem turned out to be unrelated to Carrierwave or Fog. It was an oversight on my part that the ENV variables which I'd set (in my app's .env file) were not being loaded into the bootstrapped Rails environment (e.g. rails console). Once I added http://github.com/bkeepers/dotenv (which solves precisely that issue) to my bundle, the save_to_s3 method worked.