将字节数组构造的图像上传到 Azure 而无需在本地保存

Uploading image constructed from byte array to Azure without saving locally

我有一个函数将字节数组作为参数,使用 bytesIO 并将其构造成图像,然后将其上传到 azure blob 存储。

它应该看起来像这样:

def foo(b):
  img = Image.open(io.BytesIO(b))
  container_client = ContainerClient.from_conenction_string(connection_string, container_name)
  blob_client = container_client.get_blob_client('fileUpload')
  blob_client.upload(img)

但我收到错误 seek() takes 2 positional arguments but 3 were given

我找到了这个 但无法让它为我工作。

完整的错误回溯:

Traceback (most recent call last):

  File "c:\WaitingRoom\uploadingfiles\form_recog.py", line 48, in <module>

    blob_client.upload_blob(img)

  File "C:\Users5149\AppData\Local\Programs\Python\Python39\lib\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer

    return func(*args, **kwargs)

  File "C:\Users5149\AppData\Local\Programs\Python\Python39\lib\site-packages\azure\storage\blob\_blob_client.py", line 706, in upload_blob

    options = self._upload_blob_options(

  File "C:\Users5149\AppData\Local\Programs\Python\Python39\lib\site-packages\azure\storage\blob\_blob_client.py", line 361, in _upload_blob_options

    length = get_length(data)

  File "C:\Users5149\AppData\Local\Programs\Python\Python39\lib\site-packages\azure\storage\blob\_shared\request_handlers.py", line 87, in get_length

    data.seek(0, SEEK_END)

TypeError: seek() takes 2 positional arguments but 3 were given

我尝试从图像中获取字节以用于测试目的并将这些字节上传到 azure blob 您可以使用 upload_blob 函数将图像的输入字节传递到 azure blob

import os
import io
from PIL import Image
from azure.storage.blob import BlobServiceClient
from array import array

def readimage(imagepath):
    count = os.stat(path).st_size / 2
    with open(path, "rb") as f:
        return bytearray(f.read())

bytes = readimage("pathtoimage") // getting bytes of image
image = Image.open(io.BytesIO(bytes))
print("hiiiiiiiiiiiiiiiiiiiiii")
#print(bytes)

blob_service_client = BlobServiceClient.from_connection_string("Connection String")
# Initialise container
blob_container_client = blob_service_client.get_container_client("test")
# Get blob
blob_client = blob_container_client.get_blob_client("test.jpg// blob name ")
blob_client.upload_blob(bytes) // uploaded blob with bytes 

输出

图片上传为test.jpg