简体   繁体   English

如何使用 Python 从 Amazon S3 下载 JPG 文件到 memory?

[英]How do I download a JPG file from Amazon S3 into memory using Python?

I'm struggling to download a JPG file from Amazon S3 using Python, I want to load this code onto Heroku so I need to the image to be loaded into memory rather than onto disk.我正在努力使用 Python 从 Amazon S3 下载 JPG 文件,我想将此代码加载到 Heroku 上,因此我需要将图像加载到 memory 而不是磁盘上。

The code I'm using is:我正在使用的代码是:

import boto3

s3 = boto3.client(
        "s3",
        aws_access_key_id = access_key,
        aws_secret_access_key = access_secret
    )

s3.upload_fileobj(image_conv, bucket, Key = "image_3.jpg")

new_obj = s3.get_object(Bucket=bucket, Key="image_3.jpg")

image_dl = new_obj['Body'].read()

Image.open(image_dl)

I'm getting the error message:我收到错误消息:

File ..... line 2968, in open
    fp = builtins.open(filename, "rb")
ValueError: embedded null byte

Calling image_dl returns a massive long list of what I assume are bytes, one small section looks like the following:调用 image_dl 会返回一长串我认为是字节的长列表,一小部分如下所示:

f\xbc\xdc\x8f\xfe\xb5q\xda}\xed\xcb\xdcD\xab\xe6o\x1c;\xb7\xa0\xf5\xf5\xae\xa6)\xbe\xee\xe6\xc3vn\xdfLVW:\x96\xa8\xa3}\xa4\xd8\xea\x8f*\x89\xd7\xcc\xe8\xf0\xca\xb9\x0b\xf4\x1f\xe7\x15\x93\x0f\x83ty$h\xa6\x83\xc8\x99z<K\xc3c\xd4w\xae\xa4\xc2\xfb\xcb\xee\xe0

The image before I uploaded to S3 returned the below and that's the format that I'm trying to return the image into.我上传到 S3 之前的图像返回了以下内容,这就是我试图将图像返回的格式。 Is anyone able to help me on where I'm going wrong?有谁能帮我解决我哪里出错了?

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1440x1440 at 0x7F2BB4005EB0>

Pillow's Image class needs either a filename to open, or a file-like object that it can call read on. Pillow 的图像 class 需要一个文件名才能打开,或者需要一个类似 object 的文件才能调用read Since you don't have a filename, you'll need to provide a stream. It's easiest to use BytesIO to turn the byte array into a strem:由于您没有文件名,因此您需要提供一个 stream。最简单的方法是使用BytesIO将字节数组转换为 strem:

import boto3
from PIL import Image
from io import BytesIO

bucket = "--example-bucket--"

s3 = boto3.client("s3")
with open("image.jpg", "rb") as image_conv:
    s3.upload_fileobj(image_conv, bucket, Key="image_3.jpg")
new_obj = s3.get_object(Bucket=bucket, Key="image_3.jpg")
image_dl = new_obj['Body'].read()
image = Image.open(BytesIO(image_dl))

print(image.width, image.height)

Try first to load raw data into a BytesIO container:首先尝试将原始数据加载到 BytesIO 容器中:

from io import StringIO
from PIL import Image

file_stream = StringIO()
s3.download_fileobj(bucket, "image_3.jpg", file_stream)
img = Image.open(file_stream)

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_fileobj https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_fileobj

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

相关问题 如何创建预签名 URL 以使用 Boto3 从 S3 存储桶下载文件? - How do I create a Presigned URL to download a file from an S3 Bucket using Boto3? 如何使用他们的 s3cmd 工具将文件放在 Amazon S3 存储桶中? - How do I put a file on an Amazon S3 bucket using their s3cmd tools? 我如何创建 amazon s3 url 以允许我的部署目标使用 SSM runCommand 下载由构建阶段创建的文件 - How do I create the amazon s3 url in a way that allows my deployment target to download a file created by the build stage using SSM runCommmand Python 如何从 s3 下载文件然后重新使用 - Python how to download a file from s3 and then reuse 使用 Python 从 AWS S3 下载文件 - Download file from AWS S3 using Python 如何使用 cURL 从 Amazon S3 存储桶中删除文件 - How to delete a file from amazon S3 bucket using cURL 有没有办法从网站下载 csv 文件并使用 Lambda 将其直接上传到 Amazon S3? - Is there a way to download a csv file from a website and upload it directly to Amazon S3 using Lambda? Amazon S3 更改文件下载名称 - Amazon S3 Change file download name 如何使用 python 从公共 AWS s3 下载文件? - How to download a file from public AWS s3 with python? 我如何使用 curl 从 minio s3 存储桶下载文件 - How do i download files from a minio s3 bucket using curl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM