LoginSignup
3
3

More than 5 years have passed since last update.

carrierwave + fog(Amazon S3) + RMagick で解像度によるバリデーションを行う方法

Posted at

fogを使わない場合なら、解像度によるバリデーションは以下の方法があります。

ところが、こいつをfogと組み合わせると、最初の一回しか動かず、既に画像が含まれているモデルのバリデーションをしようとすると以下のようなエラーになります。

Errno::EEXIST: File exists @ dir_s_mkdir - /home/example/example.com/releases/1/uploads
... 27 non-project frames
28
File "/home/example/example.com/releases/1/app/controllers/products_controller.rb" line 100 in add_image
... 113 non-project frames

クラスリファレンス見ると、 from_blob ってのが使えそうだったので参考記事のコードのうちconfig/initializers/carrierwave.rb を以下のような感じに書き換えると動いた。

- image = Magick::Image.read(current_path).shift
+ image = Magick::Image.from_blob(file.read).shift

備考

おそらくバリデーションする度にS3からデータを落としてきてるので、バリデーション回数が多いサービスでは気をつけた方が良いです。 EC2などS3から近いサーバーでない場合、速度にも結構な影響が出ると思われます。

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3