ruby-on-rails 为什么我的ActiveStorage服务设置被忽略?

h22fl7wq  于 2023-11-20  发布在  Ruby
关注(0)|答案(2)|浏览(136)

我在我的项目中有以下文件。

config/storage.yml

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

google:
  service: GCS
  credentials: <%= Rails.root.join("google-cloud-storage-credentials.json") %>
  project: "<%= ENV["GOOGLE_CLOUD_STORAGE_PROJECT_ID"] %>"
  bucket: "matomeishi"

字符串

config/environments/development.rb

Rails.application.configure do
  ...

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :google

config/environments/production.rb

Rails.application.configure do
  ...

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :google


由于某种原因,当尝试附加文件时,发生以下错误。

StandardError: Missing configuration for the local Active Storage service. 
Configurations available for the test and google services..


似乎应用程序没有考虑我的config.active_storage.service.有人遇到同样的问题吗?这是一个错误吗?
谢谢你,谢谢

7tofc5zh

7tofc5zh1#

经过一番检查,我终于找到了答案,看起来Rails使用的服务与之前的文件上传服务是一样的(可能是为了不破坏当前文件的URL)。

  • storage.yml的第一个版本看起来是这样的。然后我使用local服务上传了一些文件到GCS。
test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: GCS
  credentials: <%= Rails.root.join("google-cloud-storage-credentials.json") %>
  project: "<%= ENV["GOOGLE_CLOUD_STORAGE_PROJECT_ID"] %>"
  bucket: "matomeishi"

字符串

  • 在检查了我的代码之后,我将local重命名为google,因为我认为这样更合适。我还将config/development.rb中的配置从config.active_storage.service = :local更改为config.active_storage.service = :google
test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

google:
  service: GCS
  credentials: <%= Rails.root.join("google-cloud-storage-credentials.json") %>
  project: "<%= ENV["GOOGLE_CLOUD_STORAGE_PROJECT_ID"] %>"
  bucket: "matomeishi"

  • 重启我的服务器并再次尝试上传一些文件后。发生以下错误。似乎它仍然在尝试访问local服务,即使它在配置中没有提到任何地方。
StandardError: Missing configuration for the local Active Storage service. 
Configurations available for the test and google services..

原因原因是Rails首先检查active_storage_blobs表中的service_name列,并且似乎使用它而不是配置中的服务集。
解决方案

然后,我尝试将现有记录的所有service_namelocal更新为google,看看这是否能解决问题!🎉
之后,在使用ActiveStorage上传文件时,它正确地使用了google服务。

vsikbqxv

vsikbqxv2#

添加到本地环境的config/storage.yml配置
例如:

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

字符串

相关问题