本文介绍了如何使用带有S3的Rails Active Storage检索附件URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rails version 5.2

我有一种情况,我需要使用Amazon s3访问Rails Active Storage的公共URL,以使用Sidekiq后台作业制作一个zip文件.

I have a scenario that I need to access the public url of Rails Active Storage with Amazon s3 to make a zip file with Sidekiq background job.

我很难获取实际的文件url.我已经尝试关注

I am having difficulty getting the actual file url. I have tried following

rails_blob_url

但是它给了我以下的印象

but it gives me following

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

如何通过sidekiq访问真实文件的URL?

How do I access the real file url through 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

development.rb

  config.active_storage.service = :development

我可以在Web界面上访问这些内容,但不能在sidekiq内访问

I can access fine these on web interface but not within the sidekiq

推荐答案

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

Use ActiveStorage::Blob#service_url. For example, assuming a Post model with a single attached header_image:

@post.header_image.service_url

这篇关于如何使用带有S3的Rails Active Storage检索附件URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:27