繁体   English   中英

如何将本地存储(活动存储)迁移到谷歌云存储

[英]How to migrate local storage (active storage) to google cloud storage

我正在尝试在谷歌云上迁移我的 Rails 应用程序。 我已将活动存储与 GCS 上创建的存储桶连接起来。 我已经上传了存储桶中的文件夹“storage”,但应用程序中的所有图像都有 404 错误。

如何正确迁移 GCS 中的本地存储文件夹?

谢谢你的建议

我会编写一个迁移并遍历所有具有附件的模型,并使用目录中的本地文件“重新分配”当前图像,以便与 GCS 同步。 还可以查看Active Storage 指南

这个问题与this非常相似,正如该线程中提到的:

DiskService使用与谷歌云存储服务不同的文件夹结构。

DiskService 使用密钥的第一个字符的文件夹部分。 云服务只使用密钥并将所有变体放在一个单独的文件夹中。

您可以创建一个 rake 任务来将文件复制到云存储,例如:

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

执行为:rails active_storage:migrate_local_to_cloud storage_config=google。

您可以在此处找到有用的文档。

尝试使用镜像解决方案: 如何同步新的 ActiveStorage 镜像? — 先镜像,然后同步。

这适用于我从本地到 s3 服务的迁移。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM