繁体   English   中英

如何使用 python sdk 将字符串直接上传到 azure 存储 blob?

[英]How can I directly upload a string to azure storage blob using the python sdk?

What is the best way to upload a JSON string (created in memory, not read from a file) to azure blob storage using the python sdk?

我从https://pypi.org/project/azure-storage-blob/的文档中了解到,我可以通过执行以下操作将文件上传到 azure blob 存储。

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")

with open("./SampleSource.txt", "rb") as data:
    blob.upload_blob(data)

但我很难找到直接上传字符串的任何示例。

可以直接上传字符串吗? 如果是这样,它是如何在 python 中完成的?

谢谢。

您可以简单地将字符串传递给upload_blob方法。 不需要做任何特别的事情。

例如,下面的代码应该可以正常工作。

blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")

data = "This is a test"
blob.upload_blob(data)

If you're trying to upload a json object and not a json formatted string, you'll first need to turn the json obj into a string so that BlobClient can upload. 例如,如果从 api 获取 json obj...

response = requests.get("<some endpoint>")
blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")

data = json.dumps(response.json())
blob.upload_blob(data)

暂无
暂无

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

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