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

标签 ruby-on-rails amazon-s3 rails-activestorage

早些时候我的文件上传到存储文件夹中。但现在我想在 s3 存储桶上上传图像。如何迁移 s3 存储桶上现有的本地数据?

我在这里找到了脚本 https://www.stefanwienert.de/blog/2018/11/05/active-storage-migrate-between-providers-from-local-to-amazon/
但是得到一个错误

NoMethodError (private method `open' called for Active Storage



那么我应该怎么做才能将我的本地数据迁移到 s3 存储桶?

有没有更简单的方法?

最佳答案

嗨,我也遇到了那个错误,我已经将脚本更改为 rake 任务,如下所示:

# 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

关于ruby-on-rails - Rails Active Storage - 如何将本地文件迁移到 s3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735855/

相关文章:

mysql - 在这种情况下我应该使用多个数据库吗(Ruby on Rails)

javascript - 如何从 javascript aws-sdk 获取预签名 url

ruby-on-rails - 事件存储/清理文件 url

ruby-on-rails - 回形针 aws-sdk 错误 : uninitialized constant

ruby-on-rails - rails : error in setup google-api-client

ruby-on-rails - 以其他方式创建记录时,Rails 关联回调未运行

java - S3文件上传困惑

amazon-web-services - Boto3 - 创建 S3 'object created' 通知以触发 lambda 函数

ruby-on-rails - rails active_storage :install IS NOT WORKING

ruby-on-rails - 带有多个文件的 Rails file_field_tag 向 Controller 提供随机字符串