用CarrierWave Ruby gem将s3 bucket复制到另一个AWS帐户

btxsgosb  于 2023-02-03  发布在  Ruby
关注(0)|答案(1)|浏览(100)

我试图将一个bucket中的内容复制到另一个aws帐户中的bucket中,我开始将上传对象加载到一个哈希值中,然后尝试连接到另一个bucket并使用该bucket的凭据保存资产。

task :product_color_images => :environment do
  CarrierWave.configure do |c|
    c.fog_credentials = {
      :provider               => 'AWS',
      :aws_access_key_id      => ENV['COPY_FROM_AWS_KEY_ID'],
      :aws_secret_access_key  => ENV['COPY_FROM_AWS_KEY']
    }

    c.fog_directory = 'orig-bucket' # bucket copied from
  end

  image_storage = {}

  ProductImage.all.each do |image|
    puts 'storing product image'
    image_storage[image.id] = image.image
  end

  CarrierWave.configure do |c|
    c.reset_config
    c.fog_credentials = {
      :provider               => 'AWS',
      :aws_access_key_id      => ENV['COPY_TO_AWS_KEY_ID'],
      :aws_secret_access_key  => ENV['COPY_TO_AWS_KEY']
    }

    c.fog_directory = 'target-bucket' # bucket copied to
  end

  image_storage.each do |k, v|
    image = ProductImage.find(k)
    image.image = v
    puts 'saving product image'
    image.save
  end
end

尝试在控制台中将单个图像从一个存储桶保存到另一个存储桶时,会发现目标存储桶的地址未被使用。

ruby-1.9.2-p290 :026 > image = ProductImage.find(197) 
ruby-1.9.2-p290 :027 > image.image = image_storage[197]
 => https://orig-bucket.s3.amazonaws.com/uploads/product_image/image/197/product_image.png 
ruby-1.9.2-p290 :028 > image.save
ruby-1.9.2-p290 :029 > image.image
 => https://orig-bucket.s3.amazonaws.com/uploads/product_image/image/197/product_image.png
xfb7svmp

xfb7svmp1#

有时候,桶会被赋予足够的权限,也要确保你有足够的权限给图像,以便你可以真正下载它们。
我有一个更好的解决方案,你可以安装和配置你的s3cmd,然后在两个bucket之间进行rsync,这会比你的ruby on rails做得更快。

相关问题