![](/img/trans.png)
[英]Can we copy the files and folders recursively between aws s3 buckets using boto3 Python?
[英]Can we copy image from AWS S3 and write it into excel file (S3) without storing the data locally using Python?
我有一個存儲在 s3 中的 jpeg 圖像。 我的方案是創建一個插入該圖像的 excel 文件,並將該 excel 文件加載到 s3 中,而不是使用 Python 在本地存儲該 excel 文件。
您可以使用 xlsxwriter 包來完成。
import xlsxwriter
import pandas as pd
import boto3
from io import BytesIO
boto_object = boto3.resource('s3',region_name='us-west-2', aws_access_key_id=AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=AWS_SERVER_SECRET_KEY)
bucket_object = boto_object.Bucket(bucket_name=XXXX)
img_path = "images/data/sample_image.jpeg" # can be PNG, JPG or BMP
with BytesIO() as output:
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
pandas_dataframe.to_excel(writer, sheet_name='sample_sheetname') #creating a sheet first
image_obj = bucket_object.Object(img_path)
response = image_obj.get()
image_data = BytesIO(response['Body'].read())
writer.sheets['sample_sheetname'].insert_image('A1',img_path, options = {'x_scale': 0.5,
'y_scale': 0.5,
'x_offset': 80,
'y_offset': 10,'image_data':image_data}) #adding data to existing writer object
data = output.getvalue()
bucket_object.put_object(Key='<path>/sample_output.xlsx', Body=data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.