如何在python中使用pptx, io和boto3上传.pptx ?



我使用这个问题的回答,以便使用boto3和io从s3读取。pptx(一个模板)。我更新了。pptx,现在我想用一个新名字把它上传到s3。

我看到boto3有一个方法来上传一个类似文件的对象到s3:upload_fileobj()

这就是我在保存文件时所做的:

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))
# Do some stuff to the PowerPoint
out = io.BytesIO() # to have the presentation in binary format
with open(prs.save(out), "rb") as f:
s3.upload_fileobj(f, 'BUCKET', 'KEY')

但是我得到了错误

TypeError                                 Traceback (most recent call last)
<ipython-input-8-1956d13a7556> in <module>
----> 1 with open(prs.save(out), "rb") as f:
2     s3.upload_fileobj(f, 'BUCKET', 'KEY')
TypeError: expected
str, bytes or os.PathLike object, not NoneType

如果我从一个Presentation对象开始,我怎么能把它上传到s3 ?

试试这个:

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))
# Do some stuff to the PowerPoint
with io.BytesIO() as out:
prs.save(out)
out.seek(0)
s3.upload_fileobj(out, 'BUCKET', 'KEY')

最新更新