How to retrieve attachment url with Rails Active Storage with S3

后端 未结 5 1334
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 20:19
rails version 5.2

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 bac

5条回答
  •  生来不讨喜
    2020-12-13 20:52

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

    @post.header_image.service_url
    

    Update: Rails 6.1

    Since Rails 6.1 ActiveStorage::Blob#service_url is deprecated in favor of ActiveStorage::Blob#url.

    So, now

    @post.header_image.url
    

    is the way to go.

    Sources:

    • Link to the corresponding PR.
    • Link to source.

提交回复
热议问题