ruby-on-rails Rails活动存储-如何将本地文件迁移到s3存储桶

efzxgjgh  于 2022-12-15  发布在  Ruby
关注(0)|答案(6)|浏览(175)

之前我的文件是上传到存储文件夹的,但是现在我想上传图片到s3 bucket,我怎样才能把我现有的本地数据迁移到s3 bucket呢?
我在这里找到了脚本https://www.stefanwienert.de/blog/2018/11/05/active-storage-migrate-between-providers-from-local-to-amazon/,但得到了错误
NoMethodError(为活动存储调用了私有方法'open
那么我应该怎么做才能把本地数据迁移到s3 bucket呢?
有更简单的方法吗?

jdzmm42g

jdzmm42g1#

基于Dorian的回答,我构造了一个更简单的版本,对于这个版本,你不需要选择你的类或者知道/关心类中的方法是如何被调用的。
它 * 应该 * 也适用于has_many_attached(因为我们从附件本身开始)
就像Dorian的版本一样,您需要在此之前添加s3的配置并进行部署

ActiveStorage::Attachment.find_each do |at|
  next unless at.blob.service_name == "local"
  begin
    blob = at.blob
    blob.open do |f|
      at.record.send(at.name).attach(io: f, content_type: blob.content_type, filename: blob.filename)
    end
    blob.destroy
  rescue ActiveStorage::FileNotFoundError
    # Add some message or warning here if you fancy
  end
end
esbemjvw

esbemjvw2#

嗨,我也得到了这个错误,我已经改变了脚本耙任务如下:

# frozen_string_literal: true

namespace :active_storage do
  desc 'Migrate ActiveStorage files from local to Amazon S3'
  task migrate: :environment do
    module ActiveStorage
      class Downloader
        def initialize(blob, tempdir: nil)
          @blob    = blob
          @tempdir = tempdir
        end

        def download_blob_to_tempfile
          open_tempfile do |file|
            download_blob_to file
            verify_integrity_of file
            yield file
          end
        end

        private

        attr_reader :blob, :tempdir

        def open_tempfile
          file = Tempfile.open(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter], tempdir)

          begin
            yield file
          ensure
            file.close!
          end
        end

        def download_blob_to(file)
          file.binmode
          blob.download { |chunk| file.write(chunk) }
          file.flush
          file.rewind
        end

        def verify_integrity_of(file)
          raise ActiveStorage::IntegrityError unless Digest::MD5.file(file).base64digest == blob.checksum
        end
      end
    end

    module AsDownloadPatch
      def open(tempdir: nil, &block)
        ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block)
      end
    end

    Rails.application.config.to_prepare do
      ActiveStorage::Blob.send(:include, AsDownloadPatch)
    end

    def migrate(from, to)
      configs = Rails.configuration.active_storage.service_configurations
      from_service = ActiveStorage::Service.configure from, configs
      to_service   = ActiveStorage::Service.configure to, configs

      ActiveStorage::Blob.service = from_service

      puts "#{ActiveStorage::Blob.count} Blobs to go..."
      ActiveStorage::Blob.find_each do |blob|
        print '.'
        file = Tempfile.new("file#{Time.now}")
        file.binmode
        file << blob.download
        file.rewind
        checksum = blob.checksum
        to_service.upload(blob.key, file, checksum: checksum)
      rescue Errno::ENOENT
        puts 'Rescued by Errno::ENOENT statement.'
        next
      end
    end

    migrate(:local, :s3)
  end
end
nwsw7zdq

nwsw7zdq3#

一个更简单的工作方式:

  • 添加一个amazon部分到你的config/storage.yml

喜欢

  • config/environments/production.rb中将存储服务更改为:amazon
  • 将此瑞克任务添加为lib/tasks/storage.rake
namespace :storage do
  task reupload: :environment do
    [User, Event].each do |clazz|
      collection = clazz.with_attached_image

      puts "#{clazz} has #{collection.count} images"

      collection.find_each do |user|
        next unless user.image.blob
        user
          .image
          .blob
          .open do |f|
            user.image.attach(io: f, filename: user.image.blob.filename)
          end

        print "."
      end

      puts
    end
  end
end
  • 在本地运行并测试,以使用rake storage:reupload进行尝试(并在config/environments/development.rb中更改为config.active_storage.service = :amazon
  • 检查本地是否一切正常(您的图像应链接到AWS URL)
  • 上传到你的服务器(例如cap deploy
  • 在项目的目录中运行RAILS_ENV=production bundle exec rake storage:reupload
  • 等待一段时间,这取决于你重新上传的图像数量
  • 利润!享受!派对时间!
    优点
  • 别给猴子打补丁
  • 不依赖于Rails内部组件,例如https://github.com/rails/rails/issues/41636
  • 简单码
    缺点
  • 没有使其适用于has_many_attached,但不应进行太多更改
  • 你必须选择你的模特
  • 它假设所有文件都命名为image(没有什么太难修复的)
9ceoxa92

9ceoxa924#

namespace :active_storage do
  desc 'Migrate ActiveStorage files from local to minio'
  task migrate: :environment do
    module ActiveStorage
      class Downloader
        def initialize(blob, tempdir: nil)
          @blob    = blob
          @tempdir = tempdir
        end

        def download_blob_to_tempfile
          open_tempfile do |file|
            download_blob_to file
            verify_integrity_of file
            yield file
          end
        end

        private

        attr_reader :blob, :tempdir

        def open_tempfile
          file = Tempfile.open(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter], tempdir)

          begin
            yield file
          ensure
            file.close!
          end
        end

        def download_blob_to(file)
          file.binmode
          blob.download { |chunk| file.write(chunk) }
          file.flush
          file.rewind
        end

        def verify_integrity_of(file)
          raise ActiveStorage::IntegrityError unless Digest::MD5.file(file).base64digest == blob.checksum
        end
      end
    end

    module AsDownloadPatch
      def open(tempdir: nil, &block)
        ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block)
      end
    end

    Rails.application.config.to_prepare do
      ActiveStorage::Blob.send(:include, AsDownloadPatch)
    end

    def migrate(from, to)
        config_file = Rails.root.join("config/storage.yml")
      configs = ActiveSupport::ConfigurationFile.parse(config_file)
      from_service = ActiveStorage::Service.configure from, configs
      to_service   = ActiveStorage::Service.configure to, configs

      ActiveStorage::Blob.service = from_service

      puts "#{ActiveStorage::Blob.count} Blobs to go..."
      ActiveStorage::Blob.find_each do |blob|
        print '.'
        file = Tempfile.new("file#{Time.now}")
        file.binmode
        file << blob.download
        file.rewind
        checksum = blob.checksum
        to_service.upload(blob.key, file, checksum: checksum)
      rescue Errno::ENOENT
        puts 'Rescued by Errno::ENOENT statement.'
        next
      end
    end

    migrate(:local, :minio)
  end
end
yqhsw0fo

yqhsw0fo5#

将本地文件迁移到s3-service的更简单方法是:

ekqde3dh

ekqde3dh6#

基于RobbeVP的答案,我让它工作与以下任务:

namespace :active_storage do
  task reupload_to_s3: :environment do
    raise "Please switch the active storage service to 'amazon' first" if ENV['ACTIVE_STORAGE_SERVICE'] != 'amazon'
    ActiveStorage::Attachment.find_each do |at|
      next unless at.blob.service_name == "local"
      begin
        blob = at.blob
        blob.open do |f|
          at.record.send(at.name).attach(io: f, content_type: blob.content_type, filename: blob.filename)
        end
      rescue ActiveStorage::FileNotFoundError
        puts "FileNotFoundError: ActiveStorage::Attachment##{at.id}"
      end
    end
  end

  task delete_local_attachments: :environment do
    ActiveStorage::Attachment.find_each do |at|
      next unless at.blob.service_name == "local"
      at.purge
    end
  end
end

相关问题