How to retrieve attachment url with Rails Active Storage with S3

后端 未结 5 1310
被撕碎了的回忆
被撕碎了的回忆 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 21:12

    A bit late, but you can get the public URL also like this (assuming a Post model with a single attached header_image as in the example above):

    @post.header_image.service.send(:object_for, @post.header_image.key).public_url
    

    Update 2020-04-06

    1. You need to make sure, that the document is saved with public ACLs (e.g. setting the default to public)

    2. rails_blob_url is also usable. Requests will be served by rails, however, those requests will be probably quite slow, since a private URL needs to be generated on each request. (FYI: outside the controller you can generate that URL also like this: Rails.application.routes.url_helpers.rails_blob_url(@post, only_path: true))

提交回复
热议问题