Created
April 15, 2020 17:25
-
-
Save kylefritz/03c9dc176edb638dd337b5daafe34e03 to your computer and use it in GitHub Desktop.
Download blobs from s3 so can serve from active_storage.service = :local
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-s3' | |
namespace :s3 do | |
desc "Download blobs from s3 so can serve from active_storage.service = :local" | |
task :download => :environment do | |
s3 = Aws::S3::Client.new | |
bucket = '<bucket_name>' # TODO: your bucket name here | |
objects = s3.list_objects(bucket: bucket).contents | |
# skip the variants; they are made on demand for :local | |
objects = objects.reject {|o| o.key.starts_with?("variants")} | |
puts "Downloading #{objects.size} from s3 to local disk" | |
objects.each_with_index do |object, index| | |
# store blobs using same folder scheme as :local | |
storage_path = Rails.root.join("storage/#{object.key[0..1]}/#{object.key[2..3]}") | |
storage_path.mkpath | |
s3.get_object( | |
response_target: storage_path.join(object.key), | |
bucket: bucket, | |
key: object.key | |
) | |
puts "#{index + 1} / #{objects.size}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment