2010-10-14 57 views
9

我正在使用Paperclip gem來調整上傳照片的大小並將它們存儲在亞馬遜S3上。在上傳請求的生命週期中,我需要訪問調整大小的照片以傳遞給另一個Web服務。在heroku上使用s3存儲選項時訪問回形針temp文件

我懷疑在照片上傳到s3之前,有一個臨時文件在imagemagik使用的地方創建。我怎樣才能訪問它。

回答

13

根據Paperclip readme有幾個回調,它在處理之前和之後調用。

對於每個附件:

  • 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 
+1

謝謝Matt,這看起來就像我在找的東西。我縫合的部分缺少的是queued_for_write方法。我明天再試一試,然後回報。 – 2010-10-14 21:38:13

+0

完美工作。謝謝! – 2010-10-18 23:53:56

相關問題