Move Active Storage on S3 locally

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.

Directory structure

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.

procedure

Copy files from S3

Copy it to a directory called storage under tmp.

$ mkdir tmp/storage
$ aws s3 cp s3://<bucket-name> ./tmp/storage --recursive

Move to Active Storage directory

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

Clean up

Delete the copied files and working directories.

Recommended Posts

Move Active Storage on S3 locally
Introduce Active Storage
About Active Storage
About DelegationError (Active Storage)
Active Storage Initialization Manual
[Ruby on Rails] Delete s3 images with Active Strage
Procedure to use S3 of LocalStack for Active Storage
[Beginner] About Active Storage
[Rails] Create API to download files with Active Storage [S3]
Post videos with Active Storage
Upload multiple images to Cloudinary on Active Storage and publish to Heroku