Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: save file from URL and save it to Amazon S3

What's the more straightforward way to download a file from given URL and uploading it immediately to Amazon S3 (+ save into database some information about the file, like name, size etc)?

Right now, I am not using Paperclip neither Carrierwave.

Thank you

like image 867
user984621 Avatar asked Mar 14 '13 14:03

user984621


1 Answers

Straightforward:

require 'open-uri'
require 's3'

amazon = S3::Service.new(access_key_id: 'KEY', secret_access_key: 'KEY')
bucket = amazon.buckets.find('image_storage')
url = 'http://www.example.com/url'
download = open(url)

file = bucket.objects.build('image.png')
file.content = (File.read download)

if file.save
  # Make a new ActiveRecord::Base class for this
  LogFile.create(size: download.size, type: download.type, name: url)
end

https://github.com/qoobaa/s3

like image 195
ichigolas Avatar answered Oct 14 '22 08:10

ichigolas