python - 从存储桶(Google Cloud Storage)获取文件类型 json 以上传并保存在 VM 实例中
问题描述
为了从存储桶中读取新文件(json 内容)并使用 Cloud Functions 将其发送到 vm 实例,我尝试了以下代码,但出现以下错误。
import requests
import json
import ndjson
from google.cloud import storage
def hello_gcs(data, context):
"""Background Cloud Function to be triggered by Cloud Storage.
Args:
data (dict): The Cloud Functions event payload.
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the file is sent as a request to
"""
print('Bucket: {}'.format(data['bucket']))
print('File: {}'.format(data['name']))
client = storage.Client()
bucket = client.get_bucket(format(data['bucket']))
blob = bucket.get_blob(format(data['name']))
contents = blob.download_as_string()
headers = {
'Content-type': 'application/json',
}
data = ndjson.loads(contents)
print(data)
response = requests.post('10.0.0.2', headers=headers, data=data)
return "Request has been sent"
Error:
Traceback (most recent call last): 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 30, in hello_gcs data = ndjson.loads(contents) File "/env/local/lib/python3.7/site-packages/ndjson/api.py", line 14, in loads return json.loads(*args, **kwargs) File "/opt/python3.7/lib/python3.7/json/__init__.py", line 361, in loads return cls(**kw).decode(s) File "/env/local/lib/python3.7/site-packages/ndjson/codecs.py", line 9, in decode return super(Decoder, self).decode(text, *args, **kwargs) File "/opt/python3.7/lib/python3.7/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/opt/python3.7/lib/python3.7/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
解决方案
这个错误对我来说似乎很清楚:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
您的文件可能不包含有效的 json(或者在这种情况下可能是 ndjson)。
您还发布到“internal_IP_of_vm_instance”,它永远不会是有效的网址。
推荐阅读
- rest - 当响应代码为 300 或更高时,TRestRequest.Execute 退出线程
- sql-server - 是否有可能将庞大的 SQL Server 关系数据库迁移到 Azure,通过主-主复制跨多区域进行复制?
- asp.net-core - .Net 5.0 中身份和角色的实际使用
- image - 如何在 jpeg/tiff 图像顶部添加多个评论?
- c# - 处理 AcquireTokenOnBehalfOf 请求中的 MsalUiRequiredException
- swift - 快速解除协调器模式中的控制器的最佳方法
- python - discord.ext.commands.errors.CommandInvokeError:命令引发异常:KeyError:'514026847680593922'
- python - 将参数用作新创建变量名称的一部分
- angular-material - 如何在 DatePicker 角度材料中格式化日期
- nginx - 如何在重写和代理中删除路径并在nginx中传递参数