如何在 S3 上访问 URL 的 ActiveStorage 附件?
How can I access URL of an ActiveStorage attachment at S3?
我从 paperclip 迁移到 ActiveStorage,我正在努力使用 URLs。我正在使用 service_url
,但对我不起作用,因为每次调用该函数时,它 returns 都是不同的 URL。
我正在尝试使用 Rails.application.routes.url_helpers.rails_blob_path
。在本地,它有效。我只需要访问 http://localhost:3000/rails/active_storage/disk/<etc>
,其中 rails/active_storage/disk/<etc>
是通过方法调用提供给我的。
但是我如何从我的 Amazon S3 访问它?我启用了静态网站托管,但如果我点击 http://my-production-bucket.s3-website.sa-east-1.amazonaws.com/rails/active_storage/blobs/<etc>
,我会收到错误消息。我打错了 endpoint/wrong 主机吗?我还应该怎么做才能获得独特的(不是动态的)URL,还有其他最简单的方法吗?
我的storage.yml:
amazon:
service: S3
access_key_id: <%= ENV['AWS_KEY'] %>
secret_access_key: <%= ENV['AWS_SECRET'] %>
region: 'sa-east-1'
bucket: 'production-bucket'
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
production.rb:
config.active_storage.service = :amazon
检查 service_url https://api.rubyonrails.org/v5.2.0/classes/ActiveStorage/Variant.html#method-i-service_url
的文档
Returns the URL of the variant on the service. This URL is intended to be short-lived for security and not used directly with users. Instead, the service_url should only be exposed as a redirect from a stable, possibly authenticated URL. Hiding the service_url behind a redirect also gives you the power to change services without updating all URLs. And it allows permanent URLs that redirect to the service_url to be cached in the view.
Use url_for(variant) (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL for a variant that points to the ActiveStorage::VariantsController, which in turn will use this service_call method for its redirection.
它建议您在您的应用程序上实施某种代理操作。您为该操作提供固定 url,然后将请求重定向到当前 service_url.
我从 paperclip 迁移到 ActiveStorage,我正在努力使用 URLs。我正在使用 service_url
,但对我不起作用,因为每次调用该函数时,它 returns 都是不同的 URL。
我正在尝试使用 Rails.application.routes.url_helpers.rails_blob_path
。在本地,它有效。我只需要访问 http://localhost:3000/rails/active_storage/disk/<etc>
,其中 rails/active_storage/disk/<etc>
是通过方法调用提供给我的。
但是我如何从我的 Amazon S3 访问它?我启用了静态网站托管,但如果我点击 http://my-production-bucket.s3-website.sa-east-1.amazonaws.com/rails/active_storage/blobs/<etc>
,我会收到错误消息。我打错了 endpoint/wrong 主机吗?我还应该怎么做才能获得独特的(不是动态的)URL,还有其他最简单的方法吗?
我的storage.yml:
amazon:
service: S3
access_key_id: <%= ENV['AWS_KEY'] %>
secret_access_key: <%= ENV['AWS_SECRET'] %>
region: 'sa-east-1'
bucket: 'production-bucket'
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
production.rb:
config.active_storage.service = :amazon
检查 service_url https://api.rubyonrails.org/v5.2.0/classes/ActiveStorage/Variant.html#method-i-service_url
的文档Returns the URL of the variant on the service. This URL is intended to be short-lived for security and not used directly with users. Instead, the service_url should only be exposed as a redirect from a stable, possibly authenticated URL. Hiding the service_url behind a redirect also gives you the power to change services without updating all URLs. And it allows permanent URLs that redirect to the service_url to be cached in the view.
Use url_for(variant) (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL for a variant that points to the ActiveStorage::VariantsController, which in turn will use this service_call method for its redirection.
它建议您在您的应用程序上实施某种代理操作。您为该操作提供固定 url,然后将请求重定向到当前 service_url.