【问题标题】:How to access to a Google Cloud Storage bucket from a Cloud Run service without using a local Service Account key file?如何在不使用本地服务帐户密钥文件的情况下从 Cloud Run 服务访问 Google Cloud Storage 存储桶?
【发布时间】:2021-03-16 00:24:55
【问题描述】:

我刚刚部署了一个 Cloud Run REST API 应用程序,该应用程序使用 Google Cloud Storage API 从存储桶和该存储桶内的文件夹中获取最后一个文件。这是我正在使用的代码:

import os
import logging
from flask import Flask
from flask import request, jsonify, render_template
from google.oauth2 import service_account
from google.cloud import storage
from bson.json_util import dumps


app = Flask(__name__)

storage_client = storage.Client.from_service_account_json('sa.json')

@app.route('/')
# API Version 1.0
def index():
    """Welcome to Last File API Version 1.0."""
    button_text = "Add File"
    return render_template('main.html', button_text=button_text)

@app.route("/last_file_m_individual/", methods=["GET"])
def list_m_individual_files():
    """List all files in GCP bucket."""
    bucketName = request.args.get('bucketname')
    bucketFolder = request.args.get('bucketfolder')
    bucket = storage_client.get_bucket(bucketName)
    files = bucket.list_blobs(prefix=bucketFolder)
    fileList = [file.name for file in files if '.' in file.name]
    last_file_pep = fileList[-1]
    last_file_p = last_file_pep.split("/")
    last_file = last_file_p[-1]
    return last_file

@app.errorhandler(500)
def server_error(e):
    # Log the error and stacktrace.
    logging.exception('An error occurred during a request.')
    return 'An internal error occurred.', 500

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))

如何在没有本地服务帐户密钥文件的情况下在 Google Cloud Storage 和 Cloud Run 之间进行服务到服务身份验证

提前致谢。

【问题讨论】:

  • 要添加到@Steren's answer,您可能需要为云运行服务分配它自己的服务帐户,然后将该服务帐户添加到您要使用适当的role 访问的存储桶中,例如对象管理员使用gsutil 或控制台。

标签: google-cloud-platform google-cloud-storage google-cloud-run


【解决方案1】:

在 Cloud Run 上,Cloud Storage client library for Python 将自动获取identity of the Cloud Run service 的凭据,这要归功于容器内的容器实例元数据服务器。阅读更多here

【讨论】:

    【解决方案2】:

    要为 Steren 的精彩解释提供一种实用的方法来实现这一点,您必须将代码中的行替换为以下内容

    # Existing line
    storage_client = storage.Client.from_service_account_json('sa.json')
    # New line
    storage_client = storage.Client()
    

    使用此行,服务选择上下文中提供的服务帐户。感谢 Steren 在 Cloud Run 上解释的元数据服务器。但在 Cloud Function、App Engine 和 Compute Engine(以及类似:Dataflow、Dataproc、GKE 等)上也是如此

    但是,如何在您的本地环境中执行此操作?您有 2 个解决方案

    1. 您可以在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中设置服务帐号密钥文件的路径。

    但是,就个人而言,在开发时,我绝对不建议使用服务帐户密钥文件。这是一个安全漏洞。我wrote an article on this 和另一个test the containers locally with your own user credential and thus without service account key file

    1. 正如您在提到的第二篇文章中所读到的,您可以通过命令gcloud auth application-default login 在本地环境中使用 ADC(应用程序默认凭据)。

    借助这两种解决方案,您将能够以相同的方式在本地和 Cloud Run(以及其他 GCP 服务)上运行您的代码,而无需根据运行时上下文的任何“if”。

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 2019-07-25
      • 2020-07-13
      • 1970-01-01
      • 2021-05-01
      • 2021-04-08
      • 1970-01-01
      • 2019-06-12
      相关资源
      最近更新 更多