Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从云功能中读取/写入Google工作表_Python 3.x_Google Cloud Functions_Google Cloud Storage_Google Oauth_Google Sheets Api - Fatal编程技术网

Python 3.x 从云功能中读取/写入Google工作表

Python 3.x 从云功能中读取/写入Google工作表,python-3.x,google-cloud-functions,google-cloud-storage,google-oauth,google-sheets-api,Python 3.x,Google Cloud Functions,Google Cloud Storage,Google Oauth,Google Sheets Api,我试图从google cloud函数中调用Sheets API,以便从google Sheets读取数据并写入google Sheets。我的访问凭证存储在名为creds\u bucket的google存储桶中,存储在名为My\u creds.json的文件中。我正在使用的两个电子表格都有“我的信用”电子邮件链接到它们,并且所有相关的API都已为项目启用 这是我的密码。。。。我得到一个关键错误,它说它无法识别creds\u bucket。是什么驱动了这一切 import httplib2 fro

我试图从google cloud函数中调用Sheets API,以便从google Sheets读取数据并写入google Sheets。我的访问凭证存储在名为
creds\u bucket
的google存储桶中,存储在名为
My\u creds.json
的文件中。我正在使用的两个电子表格都有“我的信用”电子邮件链接到它们,并且所有相关的API都已为项目启用

这是我的密码。。。。我得到一个关键错误,它说它无法识别
creds\u 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()
您得到的错误是由于找不到任何名为“creds\u bucket”的环境变量而导致的

要修复此问题,请尝试将该名称添加到函数中,为此,您可以:

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


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

我有意避免使用环境变量选项,因为它会给我尝试执行的操作带来安全风险。我读到在GCS存储桶中存储creds是一个很好的选择,因为它能够在静止时加密这些敏感数据。这里的另一个烦恼是,谷歌的云KMS不支持云功能,因此我的选择似乎仅限于使用GCS。在这种情况下,@mgoya给出的答案是正确的,因为您没有附加任何环境变量,所以您确实面临该错误。但是,为什么要研究环境变量?要访问您的bucket,您只需指定bucket的名称(creds_bucket),删除“os.environ”部分,它就可以正常工作。
  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'