简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM