Skip to content

Instantly share code, notes, and snippets.

@ali-ehmed
Created November 5, 2019 12:01
Show Gist options
  • Save ali-ehmed/6a149270b8698396acc78b444ea0c4e1 to your computer and use it in GitHub Desktop.
Save ali-ehmed/6a149270b8698396acc78b444ea0c4e1 to your computer and use it in GitHub Desktop.
ActiveStorage - Dump files from AWS S3 storage
namespace :storage do
desc 'Downloading All Files from AWS - S3'
task dump: :environment do
verify_environment!
ActiveStorage::Blob.establish_connection(:development)
ActiveStorage::Blob.find_each do |blob|
path = make_path_for(blob.key)
if FileTest.exists?(path)
puts "ALREADY EXISTS #{blob.key} : => #{blob.filename}"
else
puts "DOWNLOADING #{blob.key} : => #{blob.filename}"
File.open(path, 'wb') do |file|
file.write(blob.download)
rescue Aws::S3::Errors::NoSuchKey
puts "DOES NOT EXIST: => #{blob.key}"
end
end
end
end
# Restrict to run this task only on Production env because that's where we have S3 storage usually.
def verify_environment!
abort('FAILED!! -> Please run this task with RAILS_ENV=production') unless Rails.env.production?
end
def make_path_for(key)
path_for(key).tap { |path| FileUtils.mkdir_p(path) }.join(key)
end
def path_for(key)
Rails.root.join('storage').join(folder_for(key))
end
def folder_for(key)
[key[0..1], key[2..3]].join('/')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment