简体   繁体   English

Django 与谷歌云存储

[英]Django with Google Cloud Storage

I have been trying to implement Google Cloud Storage (media file uploads) for my Django app on Google App Engine using the django-storages package.我一直在尝试使用 django-storages package 在 Google App Engine 上为我的 Django 应用程序实现 Google Cloud Storage(媒体文件上传)。 I have followed all the specification given by the package.我已遵循 package 给出的所有规范。 I also set the relevant policies and roles for the service account to access the storage bucket but I keep getting the error " anonymous caller does not have storage.objects.get access to the google cloud storage object ."我还为服务帐户设置了相关策略和角色以访问存储桶,但我不断收到错误消息“匿名调用者没有 storage.objects.get access to the google cloud storage object ”。 when trying to view the images in my html template.尝试查看我的 html 模板中的图像时。

I can only view the images in the template when I include the permission for allUsers in storage bucket settings.当我在存储桶设置中包含所有用户的权限时,我只能查看模板中的图像。 Does anybody know how to fix this issue without making your bucket publicly accessible.有谁知道如何在不让您的存储桶公开访问的情况下解决此问题。

This is the screenshot of my configuration for django-storages in the settings.py file这是我在 settings.py 文件中对 django-storages 的配置截图在此处输入图像描述

  1. Please check that your credentials are correct.请检查您的凭据是否正确。 If you are using gsutil, check that the credentials stored in your.boto file are accurate.如果您使用 gsutil,请检查存储在 your.boto 文件中的凭据是否准确。 Also, confirm that gsutil is using the.boto file you expect by using the command gsutil version -l and checking the config path(s) entry.此外,通过使用命令 gsutil version -l 并检查配置路径条目,确认 gsutil 正在使用您期望的 .boto 文件。

  2. And if you are using the correct credentials, are your requests being routed through a proxy, using HTTP (instead of HTTPS)?如果您使用正确的凭据,您的请求是否通过代理路由,使用 HTTP(而不是 HTTPS)? If so, check whether your proxy is configured to remove the Authorization header from such requests.如果是这样,请检查您的代理是否配置为从此类请求中删除授权 header。 If so, make sure you are using HTTPS instead of HTTP for your requests.如果是这样,请确保您使用 HTTPS 而不是 HTTP 来满足您的请求。

    You can refer the Document for storage authentication您可以参考文档进行存储认证

You can also validate with below points additionally:您还可以另外验证以下几点:

  1. In most cases, the default service accounts are not sufficient to read/write and sign files in GCS, so you will need to create a dedicated service account.在大多数情况下,默认服务帐户不足以在 GCS 中读取/写入和签署文件,因此您需要创建一个专用服务帐户。

  2. Create a service account.创建服务帐户。

  3. Make sure your service account has access to the bucket and appropriate permissions.确保您的服务帐户有权访问存储桶和适当的权限。

  4. Ensure this service account is associated to the type of compute being used (Google Compute Engine (GCE), Google Kubernetes Engine (GKE), Google Cloud Run (GCR), etc)确保此服务帐户与正在使用的计算类型相关联(Google Compute Engine (GCE)、Google Kubernetes Engine (GKE)、Google Cloud Run (GCR) 等)

For development use cases, or other instances outside Google infrastructure:对于开发用例或 Google 基础架构之外的其他实例:

-Create the key and download abc.json file. - 创建密钥并下载 abc.json 文件。
-Ensure the key is mounted/available to your running Django app. -确保密钥已安装/可用于您正在运行的 Django 应用程序。
-Set an environment variable of GOOGLE_APPLICATION_CREDENTIALS to the - 将 GOOGLE_APPLICATION_CREDENTIALS 的环境变量设置为
path of the json file. json 文件的路径。

Alternatively, you can use the setting GS_CREDENTIALS as described in document或者,您可以使用文档中描述的设置 GS_CREDENTIALS

django-storages django-storages
IAM permissions IAM 权限

I rechecked my settings and bucket policy configs and they are all correct.I realized that by setting the bucket policy to not public accessible and to be accessed via the custom service account, I was not able to display the file on to the html (via iframe tags) using the source url "https://storage.cloud.google.com/{}/'.format(GS_BUCKET_NAME)" .我重新检查了我的设置和存储桶策略配置,它们都是正确的。我意识到通过将存储桶策略设置为不可公开访问并通过自定义服务帐户访问,我无法在 html 上显示文件(通过iframe 标签)使用源 url "https://storage.cloud.google.com/{}/'.format(GS_BUCKET_NAME)" However, I was getting the file object in return.但是,作为回报,我得到了文件 object。 I read the contents of the file and base64 encoded it then rendered the files/images onto the html page and this works.Perhaps if the bucket policy is not set to public, the source url is not meant to be accessed directly or in this case only through the authorized service account.我阅读了文件的内容并 base64 对其进行了编码,然后将文件/图像渲染到 html 页面上,这很有效。也许如果存储桶策略未设置为公共,则源 Z572D4E421E5E6B711D815E8 不直接访问。只能通过授权的服务帐户。

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

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