繁体   English   中英

如何在使用 aws lambda 和 django 时从 s3 访问谷歌电子表格 json 文件

[英]how to access google spreadsheet json file from s3 while using aws lambda with django

我正在使用django并将我的应用程序部署在aws_lambda上。 一切正常,直到我想将数据库的内容保存在google spreadsheet

问题是现在aws_lambda在生产中使用json.file

视图.py

# how i would normally do it, locally

scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]

credentials = ServiceAccountCredentials.from_json_keyfile_name("secret-json-file.json", scope)

gc = gspread.authorize(credentials)

open_google_spreadsheet = gc.open('data').sheet1

但由于我使用的是aws_lambda并且我将 json 文件存储在我的aws s3存储桶的主文件夹中。

我正在尝试这样的事情:


s3_client = boto3.client("s3")

response = s3_client.get_object(Bucket=aws_bucket, Key="secret-json-file.json")

data = response["Body"].read()

# Google spreadsheet

scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]

credentials = ServiceAccountCredentials.from_json_keyfile_name(data, scope)

我也尝试过这种方法:

AWS_ID = aws_access_key_id
AWS_KEY = aws_secret_access_key
AWS_S3_BUCKET = aws_bucket

session = boto3.session.Session(aws_access_key_id=AWS_ID, aws_secret_access_key=AWS_KEY) 

s3 = session.resource("s3")

my_s3_bucket = s3.Bucket(AWS_S3_BUCKET)

current_s3_file = []

for s3_file in my_s3_bucket.objects.all():
  if s3_file.key == "secret-json-file.json":
    current_s3_file.append(s3_file)

# Google spreadsheet

scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]

credentials = ServiceAccountCredentials.from_json_keyfile_name(current_s3_file, scope)

不幸的是,这两种方法都不成功,因为我无法再运行命令zappa update production ,它会因timeout而崩溃

以下是输出:

Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 504 response code.

任何帮助将非常感激。

导入 gspread

凭据= {“类型”:“服务帐户”,“项目ID”:“api-project-XXX”,“私人密钥ID”:“2cd ... ba4”,“私人密钥”:“-----BEGIN PRIVATE KEY---- -\nNrDyLw ... jINQh/9\n-----END 私钥-----\n", "client_email": "473000000000-yoursisdifferent@developer.gserviceaccount.com", "client_id": "473 ... hd .apps.googleusercontent.com",... }

gc = gspread.service_account_from_dict(凭证)

sh = gc.open("示例电子表格")

wks=sh.worksheet("示例工作表")

我在另一个出版物中看到了这个解决方案

Python - 使 gspread.service_account 采用 python 字典或字符串而不是文件名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM