首页 > 解决方案 > 从字典而不是文件路径设置 ["GOOGLE_APPLICATION_CREDENTIALS"]

问题描述

我正在尝试从 dict 设置环境变量,但在连接时出现错误。

#service account pulls in airflow variable that contains the json dict with service_account credentials

service_account = Variable.get('google_cloud_credentials')

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=str(service_account)

错误

    PermissionDeniedError: Error executing an HTTP request: HTTP response code 403 with body '<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.</Details></Error>'

阅读时,如果我使用并指向文件,则没有问题。

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=/file/path/service_account.json

我想知道有没有办法将 dict 对象转换为类似对象的 os 路径?我不想将 json 文件存储在容器上,而且气流/谷歌文档根本不清楚。

标签: pythongoogle-cloud-storageairflow

解决方案


Python stringio 包允许您创建由字符串支持的类似文件的对象,但这在这里没有帮助,因为此环境变量的使用者需要文件路径,而不是类似文件的对象。我认为不可能做你想做的事情。您是否有理由不想将凭据放入文件中?


推荐阅读