简体   繁体   English

OSError:尝试从s3获取图像并打开图像时,无法识别图像文件<_io.BytesIO对象位于0x00000198001B9E08>

[英]OSError: cannot identify image file <_io.BytesIO object at 0x00000198001B9E08> while trying to get images from s3 and open image

I'm trying to get a jpeg. 我正在尝试获取jpeg。 from s3 bucket but I'm getting an error. 从s3存储桶中,但出现错误。 Here is a part of the code: 这是代码的一部分:

s3_client = boto3.client('s3')
bucket = os.environ['encryption_bucket']

def encrypt_zip(event, contex):
    image_key = event['keys']
    response = s3_client.get_object(Bucket= bucket, Key=image_key)
    if response['ResponseMetadata']['HTTPStatusCode'] == 200:
        object_data = response['Body']._raw_stream.data
        print(type(object_data))
        image = Image.open(io.BytesIO(object_data))

error: 错误:

OSError: cannot identify image file <_io.BytesIO object at 0x00000198001B9E08>

second question: Try to download an object from an s3 bucket and save that image 第二个问题:尝试从s3存储桶下载一个对象并保存该图像

if 's3' in event['Records'][0]:
    s3  = event['Records'][0]['s3']
    image_key  = s3['object']['key']
    key_split = str(image_key).split('/')
    job_key = "-".join(key_split)
    bucket = s3['bucket']['name']
    with open(job_key, 'wb') as data:
         client.download_fileobj(bucket, image_key, data)
         set_log("logo image loaded successfully....", False)
    image_path = '/tmp/' + job_key
    image = Image.open(job_key)
    image.save(image_path)

ERROR : OSError: cannot identify image file '8093-C8FE-2403-490B-9050e99b-53a9-4e2f-b9af-9910d5fc9a0f.jpg'

and the download image in local. 以及本地下载图像。 it is cant loaded. 无法加载。 enter image description here 在此处输入图片说明

Based on your other questions (since deleted?), your requirements are: 根据您的其他问题(已删除?),您的要求是:

  • An array of filenames are passed into the function 文件名数组传递给函数
  • Convert / into - to simplify the filename 转换/-简化文件名
  • You wish to create a zip file with these files 您希望使用这些文件创建一个zip文件

Therefore, it would be something like: 因此,它将类似于:

import boto3
import zipfile

s3_client = boto3.client('s3')
bucket = os.environ['encryption_bucket']

def encrypt_zip(event, context):
    key_array = [key.replace('/', '-') for key in event['keys']]

    # Download objects
    for key in key_array:
        response = s3_client.download_file(bucket, key, '/tmp/' + key)

    # Zip objects
    with ZipFile('/tmp/foo.zip', 'w') as myzip:
        for key in key_array:
            myzip.write('/tmp/' + key)
        myzip.close()

    # Put zip file in S3
    s3_client.upload_file('/tmp/foo.zip', bucket, 'foo.zip')

(I haven't tested this, and I just copied the Zipfile code from the documentation so I'm not sure if it is correct.) (我尚未对此进行测试,我只是从文档中复制了Zipfile代码,所以不确定其是否正确。)

I'm also not sure what you're trying to do with the / and - thing, because it depends upon the list of keys being passed in and what the objects are called in S3. 我也不确定您要使用/-东西做什么,因为它取决于传入的键列表以及S3中调用的对象。

暂无
暂无

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

相关问题 PILLOW 抛出`OSError: cannot identify image file &lt;_io.BytesIO object at 0x08B3B060&gt;` - PILLOW throws `OSError: cannot identify image file <_io.BytesIO object at 0x08B3B060>` OSError:尝试重用下载的标头时无法识别图像文件 &lt;_io.BytesIO 对象在 0x02F41960&gt; - OSError: cannot identify image file <_io.BytesIO object at 0x02F41960> while trying to reuse downloaded header 枕头OSError:无法识别图像文件&lt;_io.BytesIO对象0x02345ED8&gt; - pillow OSError: cannot identify image file <_io.BytesIO object at 0x02345ED8> OSError:在尝试使用 Keras 模型进行预测时,无法识别图像文件 &lt;_io.BytesIO object at ... &gt; - OSError: cannot identify image file <_io.BytesIO object at ... > when trying to predict with Keras model OSError:无法识别图像文件&lt;_io.BytesIO对象位于0x00000222C8A21360&gt; - OSError: cannot identify image file <_io.BytesIO object at 0x00000222C8A21360> PIL 无法识别 io.BytesIO object 的图像文件 - PIL cannot identify image file for io.BytesIO object Python 错误:PIL.UnidentifiedImageError:无法识别图像文件 &lt;_io.BytesIO object at 0x1144e9860&gt; - Python error: PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1144e9860> UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x000002154C6AE400&gt; - UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400> 引发 UnidentifiedImageError(PIL.UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x0000018CA596D350&gt; - raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000018CA596D350> 如何解决无法识别图像文件 &lt;_io.BytesIO object at 0x 0C910BD0&gt; 错误? - How to solve cannot identify image file <_io.BytesIO object at 0x 0C910BD0> error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM