将活动存储从本地磁盘服务迁移到 gcs 云 [英] Migrate active storage from local disk service to gcs cloud

查看:52
本文介绍了将活动存储从本地磁盘服务迁移到 gcs 云的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的本地 Active Storage 文件迁移到 Google Cloud Storage.我试图将 /storage/* 的文件复制到我的 GCS Bucket - 但似乎这不起作用.

I'm trying to migrate my local Active Storage files to Google Cloud Storage. I tried to just copy the files of /storage/* to my GCS Bucket - but it seems that this does not work.

我收到 404 not found 错误,因为它正在搜索如下文件:[bucket]/variants/ptGtmNWuTE...

I get 404 not found errors cause it is searching for files like: [bucket]/variants/ptGtmNWuTE...

我的本​​地存储目录具有完全不同的文件夹结构,其中包含以下文件夹:/storage/1R/3o/NWuT....

My local storage directory has a totally different folder structure with folders like: /storage/1R/3o/NWuT....

我检索图片的方法如下:

My method to retrieve the image is as followed:

variant = attachment.variant(resize: '100x100').processed
url_for(variant)

我在这里遗漏了什么?

推荐答案

事实证明 - 又名 DiskService.本地存储使用与云服务不同的文件夹结构.真的很奇怪.

As it turns out - DiskService aka. local storage uses a different folder structure than the cloud services. Thats really weird.

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

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.

创建了一个 rake 任务以将文件复制到云服务.例如,使用 rails active_storage:migrate_local_to_cloud storage_config=google 运行它.

Created a rake task to copy files over to cloud services. Run it with rails active_storage:migrate_local_to_cloud storage_config=google 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

这篇关于将活动存储从本地磁盘服务迁移到 gcs 云的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆