LoginSignup
6
3

More than 5 years have passed since last update.

Active StorageにおいてYAML syntax errorが出る問題

Posted at

Rails 5.2以降の新機能、Active Storageに関して。
公式のガイドに沿って自作サービスに画像投稿機能を付けたところ、アップロード時に次のような500エラーが発生しました。

RuntimeError (YAML syntax error occurred while parsing /(サービスのディレクトリ)/config/storage.yml.
Please note that YAML must be consistently indented using spaces.
Tabs are not allowed. Error: (<unknown>): found unexpected ':' while scanning a plain scalar at line 21 column 27):

これは見ての通りYAMLの読み込みエラーなのですが、このときエラーの出ているconfig/storage.ymlは次のようになっていました。

config/storage.yml(間違い)
local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

google:
  service: GCS
  keyfile: {
    type: "service_account",
    project_id: <%= Rails.application.credentials.gcs[:project_id] %>,
    private_key_id: <%= Rails.application.credentials.gcs[:private_key_id] %>,
    private_key: <%= Rails.application.credentials.gcs[:private_key] %>,
    client_email: <%= Rails.application.credentials.gcs[:client_email] %>,
    client_id: <%= Rails.application.credentials.gcs[:client_id] %>,
    auth_uri: "https://accounts.google.com/o/oauth2/auth",
    token_uri: "https://accounts.google.com/o/oauth2/token",
    auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
    client_x509_cert_url: <%= Rails.application.credentials.gcs[:client_x509_cert_url] %>
  }
  project: <%= Rails.application.credentials.gcs[:project] %>
  bucket: <%= Rails.application.credentials.gcs[:bucket] %>

この時問題だったのはgoogle:の箇所です。
Google Cloud Storage用のkeyをひたすらcredentialsファイルから読み込んでいるのですが、この時keyfile:の中括弧{}内部のパラメータに関しては、どうやら""を付けなければいけないようです。

すなわち、正しい書き方は次のようになります。

config/storage.yml(正しい)
local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

google:
  service: GCS
  keyfile: {
    type: "service_account",
    project_id: "<%= Rails.application.credentials.gcs[:project_id] %>",
    private_key_id: "<%= Rails.application.credentials.gcs[:private_key_id] %>",
    private_key: "<%= Rails.application.credentials.gcs[:private_key] %>",
    client_email: "<%= Rails.application.credentials.gcs[:client_email] %>",
    client_id: "<%= Rails.application.credentials.gcs[:client_id] %>",
    auth_uri: "https://accounts.google.com/o/oauth2/auth",
    token_uri: "https://accounts.google.com/o/oauth2/token",
    auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
    client_x509_cert_url: "<%= Rails.application.credentials.gcs[:client_x509_cert_url] %>"
  }
  project: <%= Rails.application.credentials.gcs[:project] %>
  bucket: <%= Rails.application.credentials.gcs[:bucket] %>

YAMLはpythonのようにインデントの違いに敏感だったり、お作法がいろいろあるそうです。

YAMLを覚えよう - エンジニア成長日記

Railsの設定ファイルにしか使わないのでつい蔑ろにしてしまいがちですが、しっかり学ばないとダメそうですね...

6
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
6
3