如何将流作为blob添加到Azure容器(如果已编码)- python

2guxujil  于 2023-04-22  发布在  Python
关注(0)|答案(1)|浏览(120)

我要添加流(StringIO|bytes)作为blob使用python到Azure容器中。当我上传时,它似乎被编码了。所以我使用了下面的代码。

def upload_blob_stream(blob_service_client: BlobServiceClient, container_name,stream,encoding=None):
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=log_blob_name)
    if encoding:
        data = stream.decode(encoding)
    else:
        data = stream
    input_stream = io.BytesIO(data)
    blob_client.upload_blob(input_stream, blob_type="BlockBlob")

upload_blob_stream(blob_service_client,container_name,stream)

但它仍然是编码的。Screenshot
有人能帮我解决这个问题吗?

wvt8vs2t

wvt8vs2t1#

我在我的环境中尝试了下面的python代码,将流作为blob添加到Azure容器中,得到了下面的结果:

验证码:

def  uploadBlobStream(blobServiceClient: BlobServiceClient, containerName, stream, blobName):
blobClient = blobServiceClient.get_blob_client(container=containerName, blob=blobName)
inputStream = io.BytesIO(stream.encode())
blobClient.upload_blob(inputStream, blob_type="BlockBlob")

conne_string = "your_connection_string"
containerName = "newcontainer"
blobName = "testblob"
string_data = "Welcome to my school"
blobServiceClient = BlobServiceClient.from_connection_string(conne_string)
uploadBlobStream(blobServiceClient, containerName, string_data, blobName)

输出:
本地:

**Portal:**Blob成功上传到Azure Portal的容器中。

当我下载blob时,它看起来像下面的

相关问题