在heroku上使用s3存储选项时访问paperclip临时文件

Bra*_*Guy 9 ruby-on-rails heroku amazon-s3 paperclip

我正在使用Paperclip gem来调整上传照片的大小并将其存储在亚马逊S3上.在上传请求的生命周期中,我需要访问已调整大小的照片以传递给另一个Web服务.

我怀疑在将照片上传到s3之前,imagemagik在某处创建了一个临时文件.我怎样才能访问它.

Mat*_*att 13

根据Paperclip自述文件,在处理之后和之前调用了一些回调.

对于每个附件:

  • before_post_process
  • after_post_process

仅适用于特定附件:

  • before_ [附件] _post_process
  • after_ [附件] _post_process

我认为在你的情况下你应该使用其中一个after回调来获得调整大小的照片.然后你应该能够访问该文件queued_for_write.例如:

class MyModel < ActiveRecord::Base
  has_attached_file :photo, :styles => { :small => "300x300>" }
  after_post_process :send_photo

  private
  def send_photo
    path = photo.queued_for_write[:small].path
    # upload the photo to the ws here
  end

end
Run Code Online (Sandbox Code Playgroud)