【问题标题】:How to upload a PDF(using PdfPages) to AWS S3 in Python如何在 Python 中将 PDF(使用 PdfPages)上传到 AWS S3
【发布时间】:2021-05-18 05:52:45
【问题描述】:

我的代码中有一些情节,我希望将它们保存为来自 AWS 的 S3 存储中的 Pdf。

【问题讨论】:

  • 感谢您尝试向 SO 提供内容,但即使是问题+自我回答也需要是正确的问题,您不是。至少PdfPages 部分需要成为问题的一部分,然后答案就是已经有大量重复项的 s3 交互部分。

标签: python amazon-web-services amazon-s3 pdfpages


【解决方案1】:

您必须使用 BytesIO 转换所有内容。您可以使用 pp.savefig() 或 pp.savefig(fig) 在循环中添加任意数量的绘图。我假设您已经配置了凭据以在 AWS 中进行身份验证。

import io
import boto3
from datetime import datetime
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import pyplot as plt

with io.BytesIO() as output:
    with PdfPages(output) as pp:
        fig, ax = plt.subplots(figsize=(12, 6))
        plt.plot([1,2,3,4,5,6],[1,2,3,4,5,6])
        pp.savefig()
    data = output.getvalue()
    
my_bucket = 'my-buketname'
s3 = boto3.resource('s3')
dir = 'my-dir-name'
file = dir + 'test.pdf'
s3.Bucket(my_bucket).put_object(Key=file, Body=data)

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 2016-03-19
    • 2015-11-16
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2020-12-23
    相关资源
    最近更新 更多