4

我正在使用带有 3 个独立模型的 Carrierwave 将照片上传到 S3。我保留了上传器的默认设置,即将照片存储在根 S3 存储桶中。然后,我决定根据模型名称(如 /avatars、items/ 等)将它们存储在子目录中,具体取决于它们上传的模型......

然后,我注意到同名文件被覆盖,当我删除模型记录时,照片并没有被删除。

从那以后,我已经从特定于上传者的设置中更改了 store_dir,如下所示:

  def store_dir
    "items"
  end

到一个在模型 ID 下存储照片的通用的(我使用 mongo 仅供参考):

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

问题来了。我正在尝试将所有已经进入 S3 的照片移动到 S3 中正确的“目录”中。根据我的准备,S3 本身没有目录。我在执行 rake 任务时遇到问题。由于我更改了 store_dir,Carrierwave 正在查找之前上传到错误目录中的所有照片。

namespace :pics do
  desc "Fix directory location of pictures on s3"
  task :item_update => :environment do
    connection = Fog::Storage.new({
      :provider                 => 'AWS',
      :aws_access_key_id => 'XXXX',
      :aws_secret_access_key => 'XXX'
    })
    directory = connection.directories.get("myapp-uploads-dev")

    Recipe.all.each do |l|
      if l.images.count > 0
        l.items.each do |i|
          if i.picture.path.to_s != ""
            new_full_path = i.picture.path.to_s
            filename = new_full_path.split('/')[-1].split('?')[0]
            thumb_filename = "thumb_#{filename}"
            original_file_path = "items/#{filename}"
            puts "attempting to retrieve: #{original_file_path}"
            original_thumb_file_path = "items/#{thumb_filename}"
            photo = directory.files.get(original_file_path) rescue nil
            if photo
              puts "we found: #{original_file_path}"
              photo.expires = 2.years.from_now.httpdate
              photo.key = new_full_path
              photo.save
              thumb_photo = directory.files.get(original_thumb_file_path) rescue nil
              if thumb_photo
                puts "we found: #{original_thumb_file_path}"
                thumb_photo.expires = 2.years.from_now.httpdate
                thumb_photo.key = "/uploads/item/picture/#{i.id}/#{thumb_filename}"
                thumb_photo.save
              end
            end
          end
        end
      end
    end
  end
end

所以我遍历所有食谱,寻找带有照片的项目,确定旧的 Carrierwave 路径,尝试根据 store_dir 的变化用新的路径更新它。我想如果我只是用新路径更新 photo.key,它会起作用,但事实并非如此。

我究竟做错了什么?有没有更好的方法来完成这里的询问?

这是我为使其正常工作所做的工作...

namespace :pics do
  desc "Fix directory location of pictures"
  task :item_update => :environment do
    connection = Fog::Storage.new({
      :provider                 => 'AWS',
      :aws_access_key_id => 'XXX',
      :aws_secret_access_key => 'XXX'
    })
    bucket = "myapp-uploads-dev"
    puts "Using bucket: #{bucket}"
    Recipe.all.each do |l|
      if l.images.count > 0
        l.items.each do |i|
          if i.picture.path.to_s != ""
            new_full_path = i.picture.path.to_s
            filename = new_full_path.split('/')[-1].split('?')[0]
            thumb_filename = "thumb_#{filename}"
            original_file_path = "items/#{filename}"
            original_thumb_file_path = "items/#{thumb_filename}"
            puts "attempting to retrieve: #{original_file_path}"
            # copy original item
            begin
              connection.copy_object(bucket, original_file_path, bucket, new_full_path, 'x-amz-acl' => 'public-read')
              puts "we just copied: #{original_file_path}"
            rescue
              puts "couldn't find: #{original_file_path}"
            end
            # copy thumb
            begin
              connection.copy_object(bucket, original_thumb_file_path, bucket, "uploads/item/picture/#{i.id}/#{thumb_filename}", 'x-amz-acl' => 'public-read')
              puts "we just copied: #{original_thumb_file_path}"
            rescue
              puts "couldn't find thumb: #{original_thumb_file_path}"
            end

          end
        end
      end
    end
  end
end

也许不是世界上最漂亮的东西,但它确实有效。

4

1 回答 1

6

您需要直接与 S3 对象交互才能移动它们。您可能希望查看Fog gem copy_objectdelete_object它是 CarrierWave 用来与 S3 交互的。

https://github.com/fog/fog/blob/8ca8a059b2f5dd2abc232dd2d2104fe6d8c41919/lib/fog/aws/requests/storage/copy_object.rb

https://github.com/fog/fog/blob/8ca8a059b2f5dd2abc232dd2d2104fe6d8c41919/lib/fog/aws/requests/storage/delete_object.rb

于 2013-09-27T02:57:11.460 回答