Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用rake任务重命名回形针文件_Ruby On Rails_Amazon S3_Paperclip_File Rename - Fatal编程技术网

Ruby on rails 使用rake任务重命名回形针文件

Ruby on rails 使用rake任务重命名回形针文件,ruby-on-rails,amazon-s3,paperclip,file-rename,Ruby On Rails,Amazon S3,Paperclip,File Rename,我在rails项目中使用了回形针来上传一些图像,使用默认设置 path: ":class/:attachment/:id_partition/:style/:filename" 因此,所有文件都上传到AmazonS3存储桶中 现在我想/需要将文件名更改为 path: ':class/:id/:style/:hash.:extension' 这对于新上传的文件很有效,但现有文件将无法再找到。 所以我试着重复使用回形针耙刷新任务。 步骤: -使用旧路径加载所有附件,使用新路径保存,删除旧条目。

我在rails项目中使用了回形针来上传一些图像,使用默认设置

path: ":class/:attachment/:id_partition/:style/:filename"
因此,所有文件都上传到AmazonS3存储桶中

现在我想/需要将文件名更改为

path: ':class/:id/:style/:hash.:extension'
这对于新上传的文件很有效,但现有文件将无法再找到。 所以我试着重复使用回形针耙刷新任务。 步骤: -使用旧路径加载所有附件,使用新路径保存,删除旧条目。 以下是我目前的研究结果:

desc "Move renamed files"
  task :move_renamed_files => :environment do
    klass = Paperclip::Task.obtain_class
    names = Paperclip::Task.obtain_attachments(klass)
    names.each do |name|
      Paperclip.each_instance_with_attachment(klass, name) do |instance|
        attachment = instance.send(name)
        if attachment.exists?
          print "."
        else
          # No hit on new location, trying old location
          org_path = attachment.options[:org_path]
          new_path = attachment.options[:path]
          attachment.options[:path] = org_path
          if attachment.exists?
            # Save file with new name
            puts "#{attachment.url}"
            attachment.options[:path] = new_path
            instance.save(:validate => false)
            # THIS DOES NOT WORK

            #if attachment.save
            #  puts "Save OK"
            #else
            # puts "Save failed"
            #end
          else
              Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
          end


        end
      end
    end
你知道如何完成代码吗?
我完全陷进去了

我将深入研究AWS::S3库,因为您已经有了路径。如果你知道它是有效的,那就用最快的方法

AWS::S3::S3Object.renameold\u名称,新的\u名称,“bucket\u名称”

此外,这可能有助于:

已在此处创建github工作项目: