[英]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.