如何在 python 中使用 pptx、io 和 boto3 上传 .pptx?
How to upload a .pptx using pptx, io and boto3 in python?
我使用了对 的回复,以便使用 boto3 和 io 从 s3 读取 .pptx(模板)。我更新了 .pptx,现在我想用新名称将它上传回 s3。
我看到 boto3 有一个 method to upload a file-like object to 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')
我使用了对
我看到 boto3 有一个 method to upload a file-like object to 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')