How to Read .json file in python code from google cloud storage bucket

[亡魂溺海] 提交于 2020-01-16 08:44:49

问题


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')

回答1:


Here is an alternative way to reach it using the official Cloud Storage library:

# 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))


来源:https://stackoverflow.com/questions/58708081/how-to-read-json-file-in-python-code-from-google-cloud-storage-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!