使用CarrierWave Ruby gem将s3存储桶复制到另一个aws帐户

时间:2013-03-27 06:53:56

标签: ruby amazon-web-services amazon-s3 carrierwave buckets

我正在尝试将一些内容从存储桶复制到另一个aws帐户中的存储桶。我首先将上传器对象加载到哈希中。然后,我尝试连接到另一个存储桶,并使用该存储桶的凭据保存资产。

    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

1 个答案:

答案 0 :(得分:1)

有时会发生这样的情况:存储桶将获得足够的权限,同时确保您对图像有足够的权限,以便您可以实际下载它们。

我有一个更好的解决方案,你可以做的是安装和配置你的s3cmd并在两个存储桶之间进行rsync。那会比你在轨道上的红宝石做得更快。