0
votes

i'm trying to migrate my rails app on google cloud. I've connect active storage with the bucket create on GCS. I've upload the folder "storage" in the bucket but all the images in the app has 404 error.

How i can correctly migrate the local storage folder in the GCS?

Thank you in advice

2
Is your application hosted on App Engine(Standard or Flexible)? How are you calling the images currently in your app? Could you share some configuration or snippet of code?Enrique Del Valle

2 Answers

0
votes

I would write a migration and iterate over all models that have attachments and "reassign" the current image with the local file in the directory, so thats will be synced with GCS. Also have a look into the Active Storage guide.

0
votes

This question is very similar to this, as is mentioned on that thread:

DiskService uses a different folder structure than cloud storage service on google.

DiskService uses as folders part of the first chars of the key. Cloud services just use the key and put all variants in a separate folder.

You can create a rake task to copy files to cloud storage, for example:

namespace :active_storage do
  desc "Migrates active storage local files to cloud"
    task migrate_local_to_cloud: :environment do
      raise 'Missing storage_config param' if !ENV.has_key?('storage_config')

      require 'yaml'
      require 'erb'
      require 'google/cloud/storage'

      config_file = Pathname.new(Rails.root.join('config/storage.yml'))
      configs = YAML.load(ERB.new(config_file.read).result) || {}
      config = configs[ENV['storage_config']]

      client = Google::Cloud.storage(config['project'], config['credentials'])
      bucket = client.bucket(config.fetch('bucket'))

      ActiveStorage::Blob.find_each do |blob|
        key = blob.key
        folder = [key[0..1], key[2..3]].join('/')
        file_path = Rails.root.join('storage', folder.to_s, key)
        file = File.open(file_path, 'rb')
        md5 = Digest::MD5.base64digest(file.read)
        bucket.create_file(file, key, content_type: blob.content_type, md5: md5)
        file.close
        puts key
      end
    end
  end

Executed as: rails active_storage:migrate_local_to_cloud storage_config=google.

You can found useful documentation at here.