Rails API ActiveStorage: Get Public URL to display image from AWS S3 Bucket?

我只是一个虾纸丫 提交于 2020-08-08 06:45:26

问题


I have a Rails 5.2 API set up and have followed the documentation on how to attach images to a model object - that's all working fine. The problem I'm having is I want to return in a JSON object the attachment's public URL so that I can use that URL as the source in an <img src... in my React front end. Is there a way to return the actual URL from the AWS S3 bucket, where the image would show up if clicked on?

Right now, I've tried rails_blob_path, service_url, and I do get URLs in return, but neither of them actually render the image the way I'd hope. Any workarounds to this?

Again, just want the attachment's actual public URL from s3 so I can plug it in to the src attribute inside an <img> and have it display. Thanks.

My development.rb file configures config.active_stoarge.service = :amazon.

My storage.yml file has amazon configured like so: amazon: service: S3 access_key_id: <%= Rails.application.secrets.amazon[:access_key] %> secret_access_key: <%= Rails.application.secrets.amazon[:secret_key] %> region: us-east-2 bucket: my_bucket_name_here

ActiveStorage 5.2.2

Rails 5.2.2


回答1:


You should be able to use rails_blob_url or rails_blob_path to create a link to the actual file.

https://edgeguides.rubyonrails.org/active_storage_overview.html#linking-to-files



来源:https://stackoverflow.com/questions/53752070/rails-api-activestorage-get-public-url-to-display-image-from-aws-s3-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!