json - How to Read .json file in python code from google cloud storage bucket
问题描述
I'm trying to read a .json
file as dict()
in a python code from VM instance stored in google cloud storage bucket.
I tried reading json file as blob:
client = storage.Client()
bucket = client.get_bucket('bucket-id-here')
blob = bucket.get_blob('remote/path/to/file.json')
str_json = blob.download_as_string()
But I'm unable to decode the str_json
. Is my approach correct? if any other approach available please let me know.
I need something like:
# Method to load json
dict = load_json(gcs_path='gs://bucket_name/filename.json')
解决方案
这是使用官方 Cloud Storage 库访问它的另一种方法:
# Import the Google Cloud client library and JSON library
from google.cloud import storage
import json
# Instantiate a Google Cloud Storage client and specify required bucket and file
storage_client = storage.Client()
bucket = storage_client.get_bucket('bucket_name')
blob = bucket.blob('filename.json')
# Download the contents of the blob as a string and then parse it using json.loads() method
data = json.loads(blob.download_as_string(client=None))
推荐阅读
- r - ggplot 从下到上重新排列 geombar
- javascript - javascript中的平滑蛇运动
- python - GMSH:如何避免在 GMSH 中创建不需要的曲面/一维元素?
- javascript - 使用 Javascript 单击按钮后如何显示表单?为什么这段代码不起作用?
- asp.net-core-identity - 如何在 ASP.NET Core Identity 中获取/加载现有用户
- html - 如何将 aria-required 添加到 Material-UI 选择组件
- javascript - 从输入中获取值并将其转换为百分比值以用于绘图饼图中
- python-3.x - TclError:图像“pyimage3”不存在(仅在第一次运行时有效)
- php - Laravel API 控制器 | 如何在控制器的这种“更新”方法中散列密码?
- java - Android 如何使用 SimpleDateFormat 添加新日期