如何从google云存储桶读取python代码中的.json文件

如何从google云存储桶读取python代码中的.json文件,json,python-3.x,google-cloud-storage,Json,Python 3.x,Google Cloud Storage,我正在尝试将.json文件作为dict()读取到存储在google云存储桶中的VM实例的python代码中 我尝试将json文件作为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() 但是我无法解码str_json。

我正在尝试将
.json
文件作为
dict()
读取到存储在google云存储桶中的VM实例的python代码中

我尝试将json文件作为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()
但是我无法解码
str_json
。我的方法正确吗?如果有其他可用的方法,请让我知道

我需要像这样的东西:

加载json的方法 dict=load_json(gcs_path='gs://bucket\u name/filename.json')
以下是使用官方云存储库访问它的另一种方式:

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

此方法使用GCS文件系统
gcsfs
从Google云存储中读取文件

#使用gcsfs读取gcs文件
导入gcsfs
导入json
gcs\u file\u system=gcsfs.GCSFileSystem(project=“gcp\u project\u name”)
gcs_json_path=“gs://bucket\u name/path/to/file.json”
将gcs_文件_system.open(gcs_json_路径)作为f:
json_dict=json.load(f)
此方法也适用于存储在GCS中的图像,使用
skimage
as

来自skimage导入io
将gcs_文件_system.open(gcs_img_路径)作为f:
img=io.imread(f)