简体   繁体   English

如何在不使用 Pandas 的情况下从 s3 存储桶获取 excel 文件并将文件再次上传到 s3 存储桶 - Python

[英]How to get an excel file from s3 bucket and upload the file again to s3 bucket without using pandas - Python

I want to get an excel file with s3.get_object function and upload the file back to a temp location in the s3 bucket through s3.put_object function.我想使用 s3.get_object 函数获取一个 excel 文件,并通过 s3.put_object 函数将文件上传回 s3 存储桶中的临时位置。 I do not want to use pandas library or do not want to create a pandas dataframe in between this process to do so.我不想使用熊猫库或不想在此过程之间创建熊猫数据框来这样做。

The code I used so far is:我到目前为止使用的代码是:

s3 = boto3.client('s3')
bucket = 'mybucket'
key_obj = 'name of the file.xlsx'


file_obj = s3.get_object(Bucket = bucket, Key = key_obj)
file_con = file_obj.read().decode('utf-8') 
file_data = io.StringIO(file_con)


s3.put_object(file_data, Bucket=bucket, 'tmp' + '\filename.xlsx')

This code is not able to read the excel file properly.此代码无法正确读取 excel 文件。 With this I got an error:有了这个,我得到了一个错误:


UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 15-16: invalid continuation byte

when I changed the decoding from 'utf-8' to 'ISO-8859-1'.当我将解码从“utf-8”更改为“ISO-8859-1”时。 This error was dropped but the file which was written in the tmp folder was not in readable format and was not even opening up.此错误已删除,但写入 tmp 文件夹中的文件不是可读格式,甚至没有打开。

Pls suggest .请建议。 Thanks谢谢

Your code is full of mistakes , thus its not clear what you really want to do.您的代码充满错误,因此不清楚您真正想要做什么。 But to make it a valid python code which correctly downloads and uploads a file, it should be:但要使其成为正确下载和上传文件的有效 python 代码,它应该是:

s3 = boto3.client('s3')

bucket = 'mybucket'
key_obj = 'name of the file.xlsx'

file_obj = s3.get_object(Bucket = bucket, Key=key_obj)
file_con = file_obj['Body'].read()
file_data = io.BytesIO(file_con)

s3.put_object(Body=file_data, Bucket=bucket, Key='tmp' + '/filename.xlsx')

暂无
暂无

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

相关问题 python将数据而非文件上传到s3存储桶 - python upload data, not file, to s3 bucket 如何使用 python 从 Flask 的 HTML 表单上传文件到 S3 存储桶? - How to upload a file from a Flask's HTML form to an S3 bucket using python? AWS Lambda:使用Python从s3存储桶中读取csv文件尺寸,而无需使用Pandas或CSV包 - AWS Lambda: read csv file dimensions from an s3 bucket with Python without using Pandas or CSV package 如何将文件上传到 s3(使用 Python),其中 s3 存储桶名称的名称中有斜杠“/” - How to upload a file into s3 (using Python) where the s3 bucket name has a slash '/' in the name 如何在Python中将HDF5文件直接上传到S3存储桶 - How to upload HDF5 file directly to S3 bucket in Python 如何使用 Python 中的 Pandas 从 s3 存储桶中读取 csv 文件 - How to read a csv file from an s3 bucket using Pandas in Python 如何使用 boto 将文件上传到 S3 存储桶中的目录 - How to upload a file to directory in S3 bucket using boto 如何使用 Boto 将文件上传到 S3 存储桶上的确切位置 - How to upload file to exact location on S3 bucket using Boto 将文件从 databricks dbfs / local 上传到 S3 存储桶。 如何使用 boto3 库或挂载 s3 将文件从 databricks 上传到 S3 存储桶? - Uploading a file from databricks dbfs / local to an S3 bucket. How do i upload a file from databricks to S3 bucket using boto3 library or mounting s3? Python - 如何读取从 S3 存储桶检索的 CSV 文件? - Python - How to read CSV file retrieved from S3 bucket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM