繁体   English   中英

将 Pandas Dataframe 转换为镜像并直接上传到 S3 存储桶,无需本地保存使用 Python

[英]Convert Pandas Dataframe to Image and Upload Directly to S3 Bucket Without Saving Locally Using Python

我正在编写一个 Python 脚本,我将在 AWS Lamba function 中运行该脚本。 python 脚本将生成一个 pandas dataframe,我想将其转换为 a.jpeg 或 .png。 我希望将此图像文件直接写入 S3 存储桶,而无需将其保存到本地计算机上的文件夹中。 我已经对 boto3 进行了研究,并拥有 AWS 的访问密钥以从 S3 读取和写入,但我需要帮助将 pandas dataframe 转换为图像文件,然后直接上传到 s3 存储桶。

我在dataframe_image存储库上创建了一个问题,向图书馆作者提出了这个问题,下面是我对他的建议的实现:

import boto3
import dataframe_image as dfi
from io import BytesIO

# create the buffer in which to store the generated image
png_io = BytesIO()

# assuming that your dataframe is stored as `df` variable
df.dfi.export(png_io)

# create the boto3 client and upload the BytesIO directly;
# create your client in whatever way works for you
session = boto3.session.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=region_name)
s3_client = session.client('s3')
s3_client.upload_fileobj(png_io, 'your_destination_folder/your_destination_file')

有关更多信息,请查看此TechOverflow 帖子和此StackOverflow 帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM