アラフォーからのプログラミングとデザイン

大人から始めたプログラミングとデザインについてのあれこれ

ephemeralエラーを解決する

f:id:simpledancer:20190502143523p:plain

###### WARNING:
remote:
remote: You set your `config.active_storage.service` to :local in production.
remote: If you are uploading files to this app, they will not persist after the app
remote: is restarted, on one-off dynos, or if the app has multiple dynos.
remote: Heroku applications have an ephemeral file system. To
remote: persist uploaded files, please use a service such as S3 and update your Rails
remote: configuration.
remote:

やっとAWSに変えてからherokuのデプロイし直して、動いたと思ったのにエラー

どうやらこれが原因らしい。

railsgirls.jp

Ephemeral ファイルシステム

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

(各 dyno は専用の ephemeral ファイルシステムを取得します。この領域には最新のデプロイしたプログラムもコピーされます。実行中のプロセスが dyno の生存期間にある間は、このファイルシステムを一時的なスクラッチパッドとして扱う事ができます。しかし、dynoが停止か再起動をした瞬間に出力されていたすべてのファイルはプロセスから見えなくなります)

App では、追加した Idea レコードにファイルを添付する事ができます。このファイルはアプリの public/uploads フォルダ以下に配置されます。以下の手順で Heroku での ephemeral ストレージの動作を確認できます:

  1. heroku open を実行してアプリを開きます
  2. 新しい Idea に画像を付けて追加します
  3. heroku restart を実行してアプリを再起動します
  4. 追加した Idea を再度表示し、このページを reload します - 画像は表示されなくなります

github.com

Ephemeral ストレージの回避策

これは実際のアプリにとっては明らかに不便ですが、有名なサイトでも使われてる回避策がちゃんとあります。

最も一般的な回避策はAmazon S3(Simple Storage Service)やRackspace CloudFilesのような外部ホストの資源を利用する事です。これらのサービスは(安価な - 通常 0.1$/GB 以下の)アプリから永続的に利用可能なストレージを ‘クラウド上に’ 提供します(つまりファイルをどこへでも提供し得えます)。

 

で紹介されてたgithubとAWSの公式ページから調べてみることにしました。 

やはり公式ページをちゃんと読まなくては、ですね。