簡體   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