Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave add pictures through "heroku rails console" from s3 on production side

I have a simple app that uploads picture that i am saving using carrierwave in blog database. (having title,body and image) and my credentials are working fine.

I have an image uploaded in s3 account with this url: /s3.amazonaws.com/Buket_name/..path../thumb_smile.png

How can I update database with image from heroku rails console. This doesnt seem to work:

b = Blog.new
b.title = "a blog"
b.body = "some text"
b.image =  File.new("s3.amazonaws.com/Buket_name/..path../thumb_smile.png","a")
or
b.image =  File.open("s3.amazonaws.com/Buket_name/..path../thumb_smile.png","r")

Errno::ENOENT: No such file or directory - 
s3.amazonaws.com/Buket_name/..path../thumb_smile.png
like image 252
Atul Avatar asked Sep 09 '13 22:09

Atul


1 Answers

Use CarrierWave's remote_{name}_url= attribute for the easiest solution.

b = Blog.new
b.title = "a blog"
b.body = "some text"
b.remote_image_url = 'http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png'
b.save

This functionality is specific to CarrierWave, so if you are looking to do something similar with another library, use open-uri from the standard library.

require 'open-uri'
image = open('http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png')

Now image is a Tempfile that can be used like a file in your Ruby script.

require 'open-uri'
image = open('http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png')

b = Blog.new
b.title = "a blog"
b.body = "some text"
b.image = image
b.save
like image 162
Benjamin Manns Avatar answered Nov 15 '22 03:11

Benjamin Manns