Rails API ActiveStorage:获取公共URL以显示来自AWS S3存储桶的图像? [英] Rails API ActiveStorage: Get Public URL to display image from AWS S3 Bucket?

查看:104
本文介绍了Rails API ActiveStorage:获取公共URL以显示来自AWS S3存储桶的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了Rails 5.2 API,并遵循了有关如何将图像附加到模型对象的文档-一切正常.我遇到的问题是我想在JSON对象中返回附件的公共URL,以便可以在React前端的<img src...中使用该URL作为源.是否有一种方法可以从AWS S3存储桶中返回实际URL,如果单击该图像,该图像将显示在哪里?

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?

现在,我尝试了rails_blob_pathservice_url,但确实得到了URL作为回报,但是它们都不以我希望的方式实际呈现图像.有任何解决方法吗?

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?

同样,只希望附件的实际公共URL来自s3,因此我可以将其插入<img>内部的src属性中并显示它.谢谢.

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.

我的development.rb文件配置了config.active_stoarge.service = :amazon.

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

我的storage.yml文件具有如下配置的Amazon: 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

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

ActiveStorage 5.2.2

Rails 5.2.2

Rails 5.2.2

推荐答案

您应该能够使用rails_blob_urlrails_blob_path创建指向实际文件的链接.

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到文件

这篇关于Rails API ActiveStorage:获取公共URL以显示来自AWS S3存储桶的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆