简体   繁体   English

Google Cloud Storage - 在不知道特定文件名的情况下公开 URL

[英]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.

相关问题 Active Storage public_url 不返回 key/filename.extension - Active Storage public_url not returning key/filename.extension 如何将文件隐私(公共或私有)添加到谷歌云存储文件中,并在 Rails 中使用活动存储? - How to add file privacy(public or private) to google cloud storage files with active storage in rails? 获取没有文件名的URL? - Get URL without filename? Google Cloud Storage在Ruby中无故返回400错误请求 - Google Cloud Storage returning 400 bad request without reason in Ruby 使用carrierwave将图像上传到谷歌云存储,文件名最终被保存,而不是桶中图像的公共链接 - Using carrierwave to upload images to google cloud storage, the file name ends up being saved and not the public link to the image in the bucket 如何将本地存储(活动存储)迁移到谷歌云存储 - How to migrate local storage (active storage) to google cloud storage 使用带有自定义域的私有谷歌云存储 - Using a private google cloud storage with a custom domain 使用 Google Cloud Storage 和 Carrierwave 保存图像时出错 - Errors in saving image with Google Cloud Storage and Carrierwave Rails - 将私有文件上传到 Google Cloud Storage - Rails - Upload private file to Google Cloud Storage 在Rails网站上将图像上传到Google云存储 - Uploading an image to google cloud storage on a rails site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM