从 s3 读取 .pptx 文件

问题描述

我尝试从 Amazon S3 打开 .pptx 并使用 python-pptx 库读取它。这是代码

from pptx import Presentation
import boto3
s3 = boto3.resource('s3')

obj=s3.Object('bucket','key')
body = obj.get()['Body']
prs=Presentation((body))

它给出了“AttributeError: 'StreamingBody' object has no attribute 'seek'”。这不应该工作吗?我怎样才能解决这个问题?我也尝试先在身体上使用 read() 。 有没有不实际下载文件解决方案?

解决方法

要从 S3 加载文件,您应该下载(或使用流策略)并使用 io.BytesIO 来转换您的数据,因为 pptx.Presentation 可以处理。

import io
import boto3

from pptx import Presentation

s3 = boto3.client('s3')
s3_response_object = s3.get_object(Bucket='bucket',Key='file.pptx')
object_content = s3_response_object['Body'].read()

prs = Presentation(io.BytesIO(object_content))

参考:

Just like what we do with variables,data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations. journaldev