我的RoR应用程序在Heroku上,并且活动存储已正确配置。但是,当我进入应用程序并获取上传的文档时,URL类似于https://cremers.s3.eu-west-1.amazonaws.com/cx9xy0pmbieagvuw8a0vzcnfhvcc?response-content-disposition=inline%3B filename%3D"Digeste_9.1.pdf"%3B....
如何将其更改为“普通”网址,例如https://www.cremers.fr/documents/digest_9.1.pdf?
显然,我没有使用您的设置进行测试,并且我没有使用ActiveStorage,因此您可能需要/想要进行调整,但这应该可以帮助您入门:
# config/routes.rb
Rails.application.routes.draw do
get '/documents/:filename.:format.:compression', to: 'documents#show'
end
# app/controllers/documents_controller.rb
require 'open-uri'
class DocumentsController < ApplicationController
def show
bucket_name = 'cremers'
aws_region = 'eu-west-1'
filename = params[:filename]
s3_url = "https://s3-#{aws_region}.amazonaws.com/#{bucket_name}/#{filename}"
data = open(s3_url)
send_data data.read, type: data.content_type
end
end