【问题标题】:Read/Write Google Sheets from within a Cloud Function从云函数中读取/写入 Google 表格
【发布时间】:2019-11-15 16:36:33
【问题描述】:

我正在尝试从谷歌云函数中调用表格 API,以便从 Google 表格中读取数据并写入 Google 表格。我的访问凭据存储在一个名为creds_bucket 的谷歌存储桶中,位于一个名为my_creds.json 的文件中。我使用的两个电子表格都有链接到它们的“my_creds”电子邮件,并且所有相关的 API 都已为项目启用。

这是我的代码....我收到一个 KEY 错误,说它无法识别 creds_bucket 。是什么驱动了这一切?

import httplib2
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import os
import json
from google.cloud import storage

def main(data, context):

    blob = storage.Client().get_bucket(os.environ['creds_bucket']).get_blob('my_creds.json').download_as_string()

    parsed_json_creds = json.loads(blob)

    scope = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
    credentials = ServiceAccountCredentials.from_json_keyfile_name(parsed_json_creds, scope)

    service = build('sheets', 'v4', http=credentials.authorize(httplib2.Http()))

    spreadsheet_id_input = 'spreadsheetinput_ID'
    range_input = 'Sheet1!A1:D10'

    spreadsheet_id_ouput = 'SPREADHEET_Output_ID'
    range_ouput = 'Sheet1!A1:D10'

    # pull data from input google sheet
    response = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id_input, range=range_input).execute()
    # export pulled data from input sheet to output sheet
    request2 = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id_ouput, range=range_ouput, body=response)
    response2 = request2.execute()
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 10, in main
    blob = storage.Client().get_bucket(os.environ['creds_bucket']).get_blob('my_creds.json').download_as_string()
  File "/env/lib/python3.7/os.py", line 678, in __getitem__
    raise KeyError(key) from None
KeyError: 'creds_bucket'

【问题讨论】:

    标签: python-3.x google-cloud-functions google-cloud-storage google-oauth google-sheets-api


    【解决方案1】:

    您遇到的KeyError 错误是由os.environ function 未找到任何名为“creds_bucket”的环境变量引起的。

    要修复它,请尝试 attaching an environment variable 将该名称添加到函数中,您可以这样做:

    在控制台中编辑函数并在“环境”部分添加一个变量

    或者

    使用带有“--update-env-vars”标志的 gcloud 命令重新部署函数

    【讨论】:

    • 我故意避免使用环境变量选项,因为它会给我正在尝试做的事情带来安全风险。我读到将凭据存储在 GCS 存储桶中是一个不错的选择,因为它能够在静态时加密这些敏感数据。这里的另一个烦恼是 Google 的 Cloud KMS 不能与 Cloud Functions 一起使用,所以看起来我的选择仅限于使用 GCS。
    • @mgoya 的答案在这种情况下是正确的,您确实面临该错误,因为您没有附加任何环境变量。但是,为什么要查看环境变量?要访问您的存储桶,您只需指定存储桶的名称(creds_bucket),删除“os.environ”部分,它应该可以按您的意愿工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2020-03-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多