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

时间:2022-12-26 10:36:52

I have used in my rails project paperclip to upload some images, by using the default

我使用我的rails项目paperclip来上传一些图像,使用默认值

path: ":class/:attachment/:id_partition/:style/:filename"

So all files are uploaded in an amazon s3 bucket.

所以所有文件都上传到亚马逊s3桶中。

Now I want / need to change the filename to

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

path: ':class/:id/:style/:hash.:extension'

This works well for newly uploaded files, but existing files are not found any more. So I tried to re-use a paperclip rake refresh task. Steps: - Load all attachments with old path, save with new path, delete old entry. This are my results so far:

这适用于新上传的文件,但不再找到现有文件。所以我试图重新使用回形针耙刷新任务。步骤: - 使用旧路径加载所有附件,使用新路径保存,删除旧条目。这是我到目前为止的结果:

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

Any Ideas how to complete the code? I am absolut stuck in it.

任何想法如何完成代码?我绝对陷入其中。

2 个解决方案

#1


1  

I would dive in to the AWS::S3 library, since you have the paths already. If you know it works then just do it the fast way.

我会潜入AWS :: S3库,因为你已经有了路径。如果你知道它有效,那么就这么快就做。

AWS::S3::S3Object.rename(old_name, new_name, 'bucket_name')

AWS :: S3 :: S3Object.rename(old_name,new_name,'bucket_name')

Also, this might help: http://blog.magmalabs.io/2015/11/25/rename-s3-assets-after-paperclip-hashing.html

此外,这可能会有所帮助:http://blog.magmalabs.io/2015/11/25/rename-s3-assets-after-paperclip-hashing.html

#2


0  

Have created a github working project here:

在这里创建了一个github工作项目:

https://github.com/tclaus/Rename-S3-assets-after-paperclip-hashing

https://github.com/tclaus/Rename-S3-assets-after-paperclip-hashing

#1


1  

I would dive in to the AWS::S3 library, since you have the paths already. If you know it works then just do it the fast way.

我会潜入AWS :: S3库,因为你已经有了路径。如果你知道它有效,那么就这么快就做。

AWS::S3::S3Object.rename(old_name, new_name, 'bucket_name')

AWS :: S3 :: S3Object.rename(old_name,new_name,'bucket_name')

Also, this might help: http://blog.magmalabs.io/2015/11/25/rename-s3-assets-after-paperclip-hashing.html

此外,这可能会有所帮助:http://blog.magmalabs.io/2015/11/25/rename-s3-assets-after-paperclip-hashing.html

#2


0  

Have created a github working project here:

在这里创建了一个github工作项目:

https://github.com/tclaus/Rename-S3-assets-after-paperclip-hashing

https://github.com/tclaus/Rename-S3-assets-after-paperclip-hashing