Nicholas Haley
Nicholas Haley

Reputation: 4024

How to Download and Store Remote Image URLs with Carrierwave

Apologies if this question has an obvious answer, I am somewhat rusty with rails.

Some background: I am building a Rails API that is supposed to scrape recipes, store them, and serve them to my frontend Ember app. Everything seems to be working great so far, except I keep getting stuck on the images. Since these images are quite large I would like to download the images and process them, then upload them to S3. To accomplish this I thought carrierwave might do the trick, but I can't seem to feed it remote URLs. Here's where I'm at so far:

#image.rb
class Image < ActiveRecord::Base
mount_uploader :photos, PhotoUploader

belongs_to :imageable, polymorphic: true
end

#recipe.rb
class Recipe < ActiveRecord::Base
belongs_to :blog
has_many :images, as: :imageable
has_and_belongs_to_many :ingredients
end

class PhotoUploader < CarrierWave::Uploader::Base
...

In my rails console I can do things like

Recipe.first.images => []

But I am unable to populate the images array from my console with image URLs. How can I feed carrierwave remote image URLs and store them on S3?

Upvotes: 2

Views: 3044

Answers (1)

blnc
blnc

Reputation: 4404

You do it by passing the value into the remote field name.

In your example it would be remote_photos_url

So when you make a new image the code would look like this:

Image.new remote_photos_url: 'http://www.domain.com/path-to-image', recipe_id: n

Here is a link to the docs

Then as long as your S3 is setup and your CarrierWave Upload is setup it will all act the way you want.

Upvotes: 3

Related Questions