I don't think I will use it much, but I moved the files managed by Rails Active Storage from S3 to local (disk).
Make a note not to forget how to do it.
In S3, the file is stored in a bucket with a name like SU61LeyW6YcZtZaoNHSL2CTu
.
If you put it in a local directory, it looks like `` `storage / SU / 61 / SU61LeyW6YcZtZaoNHSL2CTu```. In other words, a two-level directory is created, the first layer is the first two characters of the file name, and the second layer is the directory name with the third and fourth characters.
Copy it to a directory called storage under tmp.
$ mkdir tmp/storage
$ aws s3 cp s3://<bucket-name> ./tmp/storage --recursive
Copy while creating a two-level directory with the following Ruby script. Run in the root directory of your application.
require 'fileutils'
src = "tmp/storage"
dst = "storage"
Dir.glob("#{src}/**/*").each do |source|
next unless File.file?(source)
file = File.basename(source)
dir1, dir2, = file.scan(/\A(?<dir1>..)(?<dir2>..)/).flatten
target = "#{dst}/#{dir1}/#{dir2}/#{file}"
FileUtils.mkdir_p(File.dirname(target)) unless File.exist?(File.dirname(target))
FileUtils.cp(source, target)
puts target
end
Delete the copied files and working directories.
Recommended Posts