简体   繁体   English

图像文件无法打开 Azure Blob 存储

[英]The Image file could not be opened Azure Blob Storage

Hi I have written a scraper that uploads images directly to Azure Blob Storage嗨,我写了一个刮板,可以将图像直接上传到 Azure Blob Storage

The upload is successful however upon opening the file it gives an error 'The Image file could not be opened' When saving locally it works fine, so somewhere when converting to Bytes to upload to the blob it breaks.上传成功,但是在打开文件时出现错误“无法打开图像文件”在本地保存时它工作正常,因此在转换为字节上传到 blob 时它会中断。 Please help请帮忙

I have tried converting it to a string as per the commented out code however that did not work either - Many thanks in advance我已尝试根据注释掉的代码将其转换为字符串,但这也不起作用 - 提前非常感谢

block_blob_service = BlockBlobService(account_name=AZURE_ACCOUNT_NAME, account_key=AZURE_ACCOUNT_KEY, connection_string=AZURE_CONNECTION_STRING)

r = requests.get(image_url, stream=True)
image_name='test_thumb.jpg'
output = io.BytesIO()
r.raw.decode_content = True
with Image.open(r.raw) as image:
    image.thumbnail((100, 100), Image.ANTIALIAS)
    image.save(image_name, format="JPEG")
#     thumbnail_as_string = base64.b64encode(output.getvalue())
    block_blob_service.create_blob_from_bytes(container_name="media", blob_name=image_name, blob=image.tobytes())```

SOLVED解决了

block_blob_service = BlockBlobService(account_name=AZURE_ACCOUNT_NAME, account_key=AZURE_ACCOUNT_KEY, connection_string=AZURE_CONNECTION_STRING)
def create_thumbnail(image_url, image_name):
    r = requests.get(image_url, stream=True)
    image_stream = io.BytesIO()
    r.raw.decode_content = True
    with Image.open(r.raw) as image:
        image.thumbnail((200, 200), Image.ANTIALIAS)
        image.save(image_stream, format="JPEG")
        image_stream.seek(0)
        block_blob_service.create_blob_from_bytes(container_name="media", blob_name=image_name, blob=image_stream.read())

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

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