【发布时间】:2020-04-25 11:27:42
【问题描述】:
我在 GCP 上执行云函数时遇到问题,该函数试图更新我的一些谷歌表格。我让这个脚本在 Jupyter 中工作,但很难将它虚拟部署为云函数。我的问题似乎是授权 CF 发布到谷歌表格。
我在超过 6 个多小时的时间里尝试了很多事情,大多数问题都在 stackoverflow 上,中篇文章 github,但无法为我找到可行的解决方案。我不认为这是角色/权限问题。我了解当您在云功能之外但不在其中时,其中一些可能会起作用。
最终,我认为,从我所见,最好的方法是将我的 JSON 密钥托管在存储桶中并调用它,我试过这个没有成功,这似乎有点令人费解,因为一切都来自谷歌服务。
老实说,我已经回到了我的原始代码,所以我回到了第一个错误,这只是我的 JSON 密钥无法找到,因为当我在 Jupyter 中运行它时它在同一个目录中......因此为什么我创建了一个谷歌存储桶来尝试链接。
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import google.cloud
from df2gspread import df2gspread as d2g
from df2gspread import gspread2df as g2d
import datetime
import time
import numpy as np
def myGet(event, context):
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('my-key-name.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = '--removed actual key/id--'
错误:文件“/env/local/lib/python3.7/site-packages/oauth2client/service_account.py”,第 219 行,在 from_json_keyfile_name 中,open(filename, 'r') as file_obj: FileNotFoundError: [Errno 2]没有这样的文件或目录:'my-key-name.json'
非常感谢您对此的任何指导和支持。我已经彻底查看并尝试自己解决这个问题。编辑:请记住,这不是一个存在于目录中的 .py 文件,这是我的问题的一部分,据我所知,我不知道链接到哪里,因为它是一个孤立的“云功能”。
我在 20 多次尝试解决此问题时查看了一些链接,仅举几例:
How can I get access to Google Cloud Storage using an access and a secret key
Accessing google cloud storage bucket from cloud functions throws 500 error
https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python
https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable
更新: 我意识到您可以上传文件的 zip 文件,以便在内联编辑器中显示三个文件。一开始我没有这样做,所以无法弄清楚将 JSON 密钥放在哪里。现在我可以查看它并且需要弄清楚如何在方法中调用它。
当我对 CF 进行测试运行时,我收到了一个未显示在日志中的非描述性错误,并且无法像以前那样从 Cloud Schedular 对其进行测试。我在堆栈溢出时发现了这一点,感觉我现在需要相同的版本,但对于 python 并找出从谷歌文档中调用的内容。
Cloud Functions: how to upload additional file for use in code?
【问题讨论】:
标签: python google-cloud-platform google-cloud-functions google-cloud-storage