如何将图像文件从S3存储桶直接读取到内存中? [英] How to read image file from S3 bucket directly into memory?

查看:218
本文介绍了如何将图像文件从S3存储桶直接读取到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import boto3
s3 = boto3.resource('s3', region_name='us-east-2')
bucket = s3.Bucket('sentinel-s2-l1c')
object = bucket.Object('tiles/10/S/DG/2015/12/7/0/B01.jp2')
object.download_file('B01.jp2')
img=mpimg.imread('B01.jp2')
imgplot = plt.imshow(img)
plt.show(imgplot)

,并且有效.但是它首先将文件下载到当前目录的问题.是否可以直接在RAM中读取文件并将其解码为图像?

and it works. But the problem it downloads file into current directory first. Is it possible to read file and decode it as image directly in RAM?

推荐答案

我建议使用 io模块直接将文件读取到内存中,而根本不需要使用临时文件.

I would suggest using io module to read the file directly in to memory, without having to use a temporary file at all.

例如:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import boto3
import io

s3 = boto3.resource('s3', region_name='us-east-2')
bucket = s3.Bucket('sentinel-s2-l1c')
object = bucket.Object('tiles/10/S/DG/2015/12/7/0/B01.jp2')

file_stream = io.StringIO()
object.download_fileobj(file_stream)
img = mpimg.imread(file_stream)
# whatever you need to do

如果您的数据是二进制的,也可以使用io.BytesIO.

You could also use io.BytesIO if your data is binary.

这篇关于如何将图像文件从S3存储桶直接读取到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆