LoginSignup
9
7

More than 5 years have passed since last update.

aws-sdk for Ruby 複数バージョン(v1, v2)の共存

Last updated at Posted at 2016-06-29

※ 2017/01/19更新
paperclipの5系がリリースされ、aws-sdkのv2が正式にサポートされました。(同時にv1のサポートはなくなりました)
詳細はこちらに記載されています。
https://github.com/thoughtbot/paperclip/releases/tag/v5.0.0.beta1

なので、この記事はpaperclipの4系を使っている人、もしくは何らかの理由でaws-sdkのv1とv2を共存させる必要がある人が読んでください。

新しくPaperclipの5系をインストールすると以下のメッセージが出るので、これに沿って対応してください。

Post-install message from paperclip:
##################################################
#  NOTE FOR UPGRADING FROM 4.3.0 OR EARLIER       #
##################################################

Paperclip is now compatible with aws-sdk >= 2.0.0.

If you are using S3 storage, aws-sdk >= 2.0.0 requires you to make a few small
changes:

* You must set the `s3_region`
* If you are explicitly setting permissions anywhere, such as in an initializer,
  note that the format of the permissions changed from using an underscore to
  using a hyphen. For example, `:public_read` needs to be changed to
  `public-read`.

For a walkthrough of upgrading from 4 to 5 and aws-sdk >= 2.0 you can watch
http://rubythursday.com/episodes/ruby-snack-27-upgrade-paperclip-and-aws-sdk-in-prep-for-rails-5

背景

Railsでアプリを書いていてaws-sdkからECSを操作したくなったのですが、Rails内で画像操作用gemのpaperclipのバージョン4系を使ってS3に画像を保存していて問題にあたったのでメモ。

  • paperclipはaws-sdkのv2では使えない(バグが有る)
  • ECS用のapiはaws-sdkのv2にしか存在しない

paperclipのバグ

aws-sdkはv2で後方互換を切られてしまっているので、v2の上でpaperclipを使うと以下の様なエラーが出ます。

uninitialized constant Paperclip::Storage::S3::AWS

公式のIssueには挙げられていて、一度対応されたようですが結局他に問題が出るようでRollbackされています。

さらに、paperclipのバージョン4の間は対応しないと明記されています。

Does Paperclip 4.3.1 support aws sdk v2  · Issue  2021 · thoughtbot paperclip.png

aws-sdk複数バージョンの共存

aws側も後方互換を切ってしまっている認識はあるらしく、トップレベルの名前空間を変えてくれているので両方のバージョンを共存させることができ、問題を解決できました。
v1のトップレベルはAWS、v2のトップレベルはAws(Aだけ大文字)になっています。

また、Gemも別名で切り出してくれているのでGemfileを

Gemfile
gem 'aws-sdk-v1'
gem 'aws-sdk', '~> 2.x.x'
gem 'paperclip'

とすればOKです。

今回の問題以外でも複数バージョンを共存させて順次バージョンアップに対応させることが出来るので知っておいて損はないかと思います。

Reference

AWS SDK for Ruby V2 - Qiita

9
7
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
9
7