【发布时间】: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_url:https://api.example.com/rails/active_storage/blobs/signed_id/20190131_214818%20copy%202.jpg与service_url:https://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&response-content-type=image%2Fjpeg...
标签: ruby-on-rails ruby-on-rails-5 rails-activestorage