Ruby on rails 如何使用rails(API JSON响应)从AWS S3获取对象URL(链接)

Ruby on rails 如何使用rails(API JSON响应)从AWS S3获取对象URL(链接),ruby-on-rails,json,ruby,api,amazon-s3,Ruby On Rails,Json,Ruby,Api,Amazon S3,我有一个应用程序,我可以从管理仪表板上传图像和视频。这一切都运行得很好,我唯一不能做的就是如何(是否可能)获取对象url并将其作为json响应 目前,我收到了以下回复: { images: [ "https://promoteur-api.s3.eu-west-3.amazonaws.com//rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBNQT09IiwiZXhwIjpudWxsLCJw

我有一个应用程序,我可以从管理仪表板上传图像和视频。这一切都运行得很好,我唯一不能做的就是如何(是否可能)获取对象url并将其作为json响应

目前,我收到了以下回复:

{

images: [
"https://promoteur-api.s3.eu-west-3.amazonaws.com//rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBNQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f4fdd1c0fd4b07b03b0282a63c8fa1ca31d310c6/41567.png",
]
}
我希望它是这样的:

{

images: [
"https://promoteur-api.s3.eu-west-3.amazonaws.com/AWS_OBJECT_ID",
]
}
这是我的#serializer.rb

class ProjetSerializer < ActiveModel::Serializer
  include Rails.application.routes.url_helpers
  attributes :id, :nom, :gouvernorat, :localite, :surface, :description, :en_cours, :fini,  :images
  
  def images
    object.images.map do |image|
      Rails.application.routes.url_helpers.rails_blob_url(image, only_path: true) if object.images.attached?
    end
  end

end
class ProjetSerializer
我需要直接访问图片URL,以便在我的Flitter应用程序中显示它们,如果你有更简单的方法,请让我知道


提前感谢。

看起来您的代码是正确的,也许您没有在env文件中设置主机url?如果没有,请在
development.rb

中添加
Rails.application.routes.default\u url\u options[:host]=“this\u is\u base\u url.com”
,您可以对对象服务url使用
service\u url
方法

新型丝光剂

class ProjetSerializer < ActiveModel::Serializer
  attributes :id, :nom, :gouvernorat, :localite, :surface, :description, :en_cours, :fini,  :images
  
  def images
    object.images.map do |image|
      image.service_url if object.images.attached?
    end
  end
end

谢谢@TTD,我在我的应用程序\u控制器中设置了这一行,这是我正在使用的
class ApplicationController
这样我就得到了所需url的前半部分,我所需要做的就是从s3存储中检索ObjectID,使其像这样
https://promoteur-api.s3.eu-west-3.amazonaws.com/5vf79jb3xb0qqkdhsh6jww4irpls
尝试将
rails\u blob\u url
替换为
服务\u url
给了我这个错误
坏URI(不是URI?):nil
谢谢@Kemal\u AKIN,我尝试了image.service\uURL,但它给了我一个零错误
错误URI(不是URI?):nil
我记得我以前尝试过这个方法,但无法修复此错误。tho:/i认为您正在本地开发环境中使用磁盘服务。您可以这样描述它,然后尝试
ActiveStorage::Current.host=http://localhost:3000“
这就是问题所在,我将我的活动存储从本地切换到了amazon它完成了工作我现在要在生产上检查它
config.active\u storage.service=:amazon
# config/initializers/active_storage.rb
Rails.application.config.active_storage.service_urls_expire_in = 1.hour