使用 python 读取 jpg 时出现 Azure blob UnicodeDecodeError

标签 azure python-requests azure-functions

我正在从本地目录读取 python 中的 .jpg 文件并将其转换为 base64。它工作正常。

    with open(logo, 'rb') as f:
        data = f.read()
        f.close()        
    encoded = base64.b64encode(data).decode()
    
    

现在我必须从 Azure blob 读取相同的文件并使用下面的代码。

    blob_container = 'operations'
    BlobPath = 'img\test.jpg'
    blob_service_client = azure_utility.get_blob_service_client()
    blob_client = blob_service_client.get_blob_client(container=blob_container, blob=BlobPath)
    data_dump = blob_client.download_blob().readall().decode()
    

低于错误。

    Exception: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

如何在 blob 中读取博客相同的 jpg 文件(例如 data = f.read())并传递给encoded = base64.b64encode(data).decode()

最佳答案

尝试使用这样的代码:

from azure.storage.blob import BlobClient
import base64

blob = BlobClient.from_connection_string(conn_str="your-connect-string", container_name="container_name", blob_name="blob_name")
stream = blob.download_blob()
data = stream.readall()

encoded = base64.b64encode(data).decode()

关于使用 python 读取 jpg 时出现 Azure blob UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64603053/

相关文章:

azure - 检索 ARM 模板中的当前版本以获取 secret

asp.net - 以不同模式运行多个集线器的 Azure SignalR 服务

javascript - Azure PutBlock 最大块数

python - 向请求模块添加 header

c# - azure 中何时创建了 block blob?

python - 在Python中使用requests下载多个文件

python - 为什么 aiohttp 比 run_in_executor 包装的请求慢?

Azure Functions V4 NET 6 服务总线触发器未触发

javascript - Azure 应用服务 - Nodejs ES 模块错误

asp.net - Azure 上的 MVC4 应用程序无法运行