简体   繁体   English

在 Google App Engine 上从 Google Cloud Storage for Ruby on Rails 应用程序加载图像

[英]Loading images from Google Cloud Storage for Ruby on Rails app on Google App Engine

I have a Ruby on Rails web application (deployed on Google App Engine) using Spree/Solidus core, which is connected to Google Cloud Storage for file storage.我有一个使用 Spree/Solidus 核心的 Ruby on Rails Web 应用程序(部署在 Google App Engine 上),它连接到 Google Cloud Storage 以进行文件存储。 We have an admin dashboard for our app where we can upload images related to objects and we have a webpage in which we show those images.我们的应用程序有一个管理仪表板,我们可以在其中上传与对象相关的图像,并且我们有一个显示这些图像的网页。 The images show up at first when we recently deployed the project, but they fail to show up after a while!当我们最近部署项目时,图像首先显示,但一段时间后它们无法显示! We get the following 400 error for the images shown on the console:对于控制台上显示的图像,我们收到以下 400 错误:

Failed to load resource: the server responded with a status of 400 ()

When we check the admin dashboard, the same images are still visible, but they are shown as broken on the homepage.当我们检查管理仪表板时,相同的图像仍然可见,但它们在主页上显示为损坏。 Our Google Cloud Storage bucket has public access (though I don't think it really needs it, since the app is on the same project and we have the GC_access JSON file deployed).我们的 Google Cloud Storage 存储桶具有公共访问权限(尽管我认为它并不真正需要它,因为该应用程序在同一个项目中并且我们部署了 GC_access JSON 文件)。

We are using the following code to show the images:我们使用以下代码来显示图像:

<% if image_url = image.try(:url, size)  %>
  <%= image_tag image_url %>
<% else %>
  <span class="image-placeholder <%= size %>"></span>
<% end %>

I will appreciate any help or advice on what to try to fix this issue!对于尝试解决此问题的任何帮助或建议,我将不胜感激! I'm not sure what other information may be helpful, but if you think more information on something can be helpful I can add more details.我不确定还有哪些其他信息可能会有所帮助,但如果您认为有关某事的更多信息会有所帮助,我可以添加更多详细信息。

There are multiple things you can check in order to make sure your images are correctly being allowed to be shared publicly.您可以检查多项内容,以确保您的图像被正确允许公开共享。 The first one would be to enable Bucket Uniform Level Access .第一个是启用 Bucket Uniform Level Access This type of access is general for all the objects in your bucket instead of per-object ACLs, and the objects should be set for public access .这种类型的访问适用于您存储桶中的所有对象,而不是针对每个对象的 ACL,并且应将对象设置为公共访问 The next thing to verify is whether the image URLs are being correctly created for public viewing, according to the documentation , the URLs should be in this format:接下来要验证的是是否正确创建了图像 URL 以供公众查看,根据文档,URL 应采用以下格式:

https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME

If your URL has a long string of data, it could be that you are using signed URLs on your website.如果您的 URL 包含一长串数据,则可能是您在网站上使用了签名 URL These URLs have an expiration date, which could explain why they seem to have worked at first and then they stopped showing up for the public.这些 URL 有一个到期日期,这可以解释为什么它们起初似乎有效,然后它们停止向公众显示。 In the ActiveStorage documentation , it says that the config/storage.yml file will default to private access by creating signed URLs:在 ActiveStorage 文档中,它说config/storage.yml文件将通过创建签名 URL 默认为私有访问:

By default, Active Storage assumes private access to services.默认情况下,Active Storage 承担对服务的私有访问。 This means generating signed, single-use URLs for blobs.这意味着为 blob 生成签名的、一次性使用的 URL。 If you'd rather make blobs publicly accessible, specify public: true in your app's config/storage.yml如果您想让 blob 可公开访问,请在应用程序的 config/storage.yml 中指定 public: true

This related SO thread also talks about URLs expiring and Signed URLs.这个相关的SO 线程还讨论了 URL 过期和签名 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM