微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 如何使用Rails Active Storage与S3检索附件URL

rails version 5.2

我有一个场景,我需要使用亚马逊s3访问Rails Active Storage的公共URL来制作带Sidekiq后台作业的zip文件.

我很难获得实际的文件网址.我试过以下

rails_blob_url

但它让我跟随

http://localhost:3000/rails/active_storage/blobs/eyJfcmfpbHMiOnsibWVzc2FnZSI6IkJBaHBBZUk9IiwiZXhwIjpudWxsLCJwdXIiOijibG9iX2lkIn19--9598613be650942d1ee4382a44dad679a80d2d3b/sample.pdf

如何通过sidekiq访问真实文件网址?

storage.yml

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

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

development:
  service: S3
  access_key_id: 'xxxxx'
  secret_access_key: 'xxxxx'
  region: 'xxxxx'
  bucket: 'xxxxx'

development.rb

config.active_storage.service = :development

我可以在web界面上访问这些,但不能在sidekiq中访问

解决方法

使用 ActiveStorage::Blob#service_url.例如,假设Post模型具有单个附加的header_image:
@post.header_image.service_url

原文地址:https://www.jb51.cc/ruby/267334.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐