简体   繁体   English

将 Python 列表上传到 Google Cloud Storage 并覆盖文件

[英]Upload Python List to Google Cloud Storage and Overwrite File

I have a JSON file customer_records.json in a Google Cloud Storage bucket called sales_data .我在名为sales_data的 Google Cloud Storage 存储桶中有一个 JSON 文件customer_records.json

In a Python file, I have the following list:在 Python 文件中,我有以下列表:

metrics_map = [{"State": "Austin", "City": "Texas"}, {"State": "New York", "City": "New York"}]

How can I upload this list into a Google Cloud Storage bucket and overwrite the customer_records.json file?如何将此列表上传到 Google Cloud Storage 存储桶并覆盖customer_records.json文件?

I tried to use blob.upload_from_file(metrics_map) , but this command does not take a list object.我尝试使用blob.upload_from_file(metrics_map) ,但此命令不使用列表 object。

You can use blob.upload_from_string to upload serialized JSON.您可以使用blob.upload_from_string上传序列化的 JSON。 See the Python Docs or Uploading Objects Docs and search for upload_from_string .请参阅Python 文档上传对象文档并搜索upload_from_string You can use json.dumps(metrics_map) to create a string from your JSON.您可以使用json.dumps(metrics_map)从您的 JSON 创建一个字符串。

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

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