[英]Google Cloud Storage - getting public URL without knowing specific filename
I have a Rails 5.2.3
app being hosted on Google Cloud services.我有一个 Rails
5.2.3
应用程序托管在 Google Cloud 服务上。 On my frontend, I require users to upload images of their employees, and these images get saved into a GC Storage bucket, and importantly for my question, they're saved with randomized names .在我的前端,我要求用户上传他们员工的图像,这些图像被保存到 GC 存储桶中,重要的是,对于我的问题,它们以随机名称保存。 There's hundreds of these images, and they all have what basically amounts to random 40+ character strings as names inside the buckets.
有数百个这样的图像,它们基本上都有随机的 40 多个字符串作为存储桶内的名称。
In the DB, each User has multiple Employees, and each Employee has one image attached.在数据库中,每个用户都有多个员工,每个员工都附有一张图片。
I'm trying to create an Email in which I need to include an image of one specific employee for each user.我正在尝试创建一个 Email ,其中我需要为每个用户包含一个特定员工的图像。 Right now, I'm using the
ActiveStorage::service_url
method, but the problem is that the service_url
method will return a Signed URL from GCS, which has a (by default) 5 minute TTL, meaning the image will stop being accessible after 5 minutes and users get a broken image.现在,我正在使用
ActiveStorage::service_url
方法,但问题是service_url
方法将从 GCS 返回一个签名的 URL ,它有一个(默认情况下)5 分钟的 TTL,这意味着图像将在 5 分钟后停止访问分钟和用户得到一个破碎的形象。 The image does actually show up fine initially, it just expires after 5 minutes.该图像最初确实显示得很好,它只是在 5 分钟后过期。
An example of how I'm using the service_url
method in my mailer view我如何在邮件视图中使用
service_url
方法的示例
<%= image_tag (employee.photo.service_url) if employee.photo.attached? %>
Now, the GCS docs state that to get the public URL, you just have to use the BUCKET_NAME/FILE_NAME
URL, but because there's hundreds of these files and they're saved with randomized names (I'm not the one that set that up initially, don't ask why), I simply don't have a way of doing that in a sensible way for each user, especially since this is a weekly email that goes out to each and every user denoting their staff members that are performing the best, so even for one client the email will use a different picture from week to week. Now, the GCS docs state that to get the public URL, you just have to use the
BUCKET_NAME/FILE_NAME
URL, but because there's hundreds of these files and they're saved with randomized names (I'm not the one that set that up最初,不要问为什么),我根本没有办法为每个用户以明智的方式做到这一点,特别是因为这是每周 email 发布给每个用户的,表示他们的员工正在执行最好的,所以即使对于一个客户,email 每周都会使用不同的图片。
Is there any way for me to get the public image URL for use in these emails?有什么方法可以让我获得用于这些电子邮件的公共图像 URL 吗?
My storage.yml
file我的
storage.yml
文件
google:
service: GCS
project: REDACTED
credentials: <%= Rails.application.credentials.gcs_storage_credentials.to_json %>
bucket: images-REDACTED
My production.rb
env file我的
production.rb
环境文件
config.active_storage.service = :google
config.action_mailer.asset_host = Rails.application.credentials.send(production_server_url)
My Gemfile我的宝石文件
ruby '2.6.3'
gem 'rails', '~> 5.2.3'
gem 'pg', '>= 0.18', '< 2.0'
gem 'active_storage_base64'
gem 'google-cloud-storage', '~> 1.8', require: false
You can set the expiry per URL :您可以根据 URL 设置到期时间:
image_tag(employee.photo.service_url(expires_in: 1.year.to_i))
While max's answer is a good one should you have a need for your assets eventually expiring, I needed a more permanent solution.如果您需要最终到期的资产,max 的答案是一个很好的答案,但我需要一个更持久的解决方案。 Turns out it was really simple, I simple had to call
employee.photo
and GCS seems to be spitting out a permanent URL!事实证明这真的很简单,我不得不调用
employee.photo
,而GCS 似乎吐出了一个永久的URL!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.