【问题标题】:ActiveStorage's rails_blob_url returning a link that shows a page with 404 error when openedActiveStorage 的 rails_blob_url 返回一个链接,该链接在打开时显示带有 404 错误的页面
【发布时间】:2019-02-04 04:41:59
【问题描述】:

我有一个附件,它通过序列化程序传递给前端,如下所示。

module NameOfSerializer
  class Base < ActiveModel::Serializer
    attributes :id,
               ...
               ...
               ...
               :attachment_url

    def attachment_url
      Rails.application
           .routes.url_helpers
           .rails_blob_url(object.attachment, host: ENV['HOST_URL'])
    end
  end
end

在开发和登台方面,它运行良好。我可以复制粘贴 URL,然后可以打开并查看附件。 但是在生产中,它会显示一个带有 404 错误的页面。

这是我在 AWS Elastic Beanstalk 日志中发现的。

[2019-02-04T03:50:38.733073 #16644]  INFO -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc] Started GET "/rails/active_storage/blobs/insert_signed_id_here/20190131_214818%20copy%202.jpg" at 2019-02-04 03:50:38 +0000
I, [2019-02-04T03:50:38.736005 #16644]  INFO -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc] Processing by ActiveStorage::BlobsController#show as JPEG
I, [2019-02-04T03:50:38.736056 #16644]  INFO -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc]   Parameters: {"signed_id"=>"insert the signed_id here", "filename"=>"20190131_214818 copy 2"}
D, [2019-02-04T03:50:38.737552 #16644] DEBUG -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc]   [1m[36mActiveStorage::Blob Load (0.9ms)[0m  [1m[34mSELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2[0m  [["id", 16], ["LIMIT", 1]]
I, [2019-02-04T03:50:38.737936 #16644]  INFO -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc] Completed 404 Not Found in 2ms (ActiveRecord: 0.9ms)
F, [2019-02-04T03:50:38.740120 #16644] FATAL -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc]   
F, [2019-02-04T03:50:38.740163 #16644] FATAL -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc] ActiveRecord::RecordNotFound (Couldn't find ActiveStorage::Blob with 'id'=16):
F, [2019-02-04T03:50:38.740185 #16644] FATAL -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc]   
F, [2019-02-04T03:50:38.740215 #16644] FATAL -- : [4ca0d491-66cc-4594-9db4-75da533d2fbc] activerecord (5.2.0) lib/active_record/core.rb:177:in `find'

我很惊讶地看到日志显示未找到 ID 为 16 的 Blob,但是当我在 Elastic Beanstalk 上运行 rails c 时,我找到了该 Blob。 我还通过运行attachment.service_url 测试了另一种在rails 控制台中从S3 检索文件的方法。 我能够访问附件。没有遇到 404 错误。

有什么想法吗?我之前遇到过这个问题,针对那个问题,我改用service_url,但我想知道是什么导致rails_blob_url 在生产环境中不起作用。

【问题讨论】:

  • 可以分享一下调用 rails_blob_url 时生成的url吗?
  • 我认为这不是一个好主意。不确定这是否有帮助,但这就是 URL 的外观,只需替换 example signed_id。 @F.E.A 与rails_blob_urlhttps://api.example.com/rails/active_storage/blobs/signed_id/20190131_214818%20copy%202.jpgservice_urlhttps://bucket-name.s3.ap-southeast-1.amazonaws.com/key?response-content-disposition=inline%3B%20filename%3D%2220190131_214818%20copy%202.jpg%22%3B%20filename%2A%3DUTF-8%27%2720190131_214818%2520copy%25202.jpg&amp;response-content-type=image%2Fjpeg...

标签: ruby-on-rails ruby-on-rails-5 rails-activestorage


【解决方案1】:

我刚碰到这个。这是我为我的项目修复它的方法。

首先,我将 Amazon S3 用于生产,也用于开发。第二部分至关重要。

换句话说,我的config/environments/development.rb 有这个:

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

但是,仅此还不够。出于某种原因,Rails 仍然在生成 url 时尝试使用local

解决方法是删除 local 配置。这是我的storage.yml 之后的样子:

# THE FIX: COMMENTED THIS OUT
# local:
#   service: Disk
#   root: <%= Rails.root.join("storage") %>

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: us-east-2
  bucket: xxx

重启服务器后,一切正常。

【讨论】:

    猜你喜欢
    • 2018-09-30
    • 2017-08-14
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2011-08-12
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多