使用boto3将文件上传到S3 [英] Use boto3 to upload a file to S3

查看:952
本文介绍了使用boto3将文件上传到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,用于将容器中的csv文件上载到S3存储桶,将文件复制到本地计算机,并且正在本地测试脚本,但出现错误.我仍在学习所有内容,试图了解脚本中缺少的部分,以及如何使它运行并将文件上传到S3,

I have a script to upload a csv file which is in a container to S3 bucket, I copied the file to my local machine and I'm testing the script locally, but getting errors. I'm still learning everything, trying to know what part I'm missing in the script and how I can get this running and upload the file to S3,

这是错误:

错误_1:

Traceback (most recent call last):
  File "C:/Users/U12345/IdeaProjects/xxx/s3_upload.py", line 19, in <module>
    r'C:\Users\U12345\IdeaProjects\xxx\test_' + str(current_date) + '.csv')
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\U12345\\IdeaProjects\\xxx\\test.csv' -> 'C:\\Users\\U12345\\IdeaProjects\\xxx\\test_2020-04-16 10:55:41.csv'

error_02:

File "C:/Users/U12345/IdeaProjects/xxx/s3_upload.py", line 33
    response = s3_client.put_object(Body='C:\Users\U6079325/IdeaProjects/xxx/test.csv',
                                        ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

另一个问题是我不太确定如何调用此函数,放在括号中的参数是什么,给了我不同的错误.

Another issue is I'm not very sure how to call this function, what parameter to put in the bracket, it gave me different errors.

我已经为此苦苦挣扎了近一个星期,有点沮丧,有人可以给我一些帮助或一个可以效仿的好榜样.

I've been struggling with this for almost a week now, a bit frustrated, can someone gave me some help or a good example that I can follow.

更新:

error02和最后一个问题已解决,这只是第一个错误仍不起作用,我尝试将'/','\'与'C:'一起使用,而没有'C:',所有这些均不起作用.

error02 and the last issue have been solved, it's just the first error still not working, I've trying '/', '\', with 'C:', without 'C:', all not working...

推荐答案

您需要在此处解决一些问题,所以让我们对其进行分解.

You've got a few things to address here so lets break it down a little bit.

1)调用upload_to_s3()时,需要使用声明了它的函数参数,文件名和存储桶键来调用它.因此,例如upload_to_s3(filename, bucket_key).

1) When you call upload_to_s3() you need to call it with the function parameters you've declared it with, a filename and a bucket key. So it would be upload_to_s3(filename, bucket_key) for example.

2)自从我使用Windows&以来已经有一段时间了. Python,但请问自己,它是否在文件路径中使用\而不是/,还请确保该文件肯定在您期望的位置.

2) It's a been a while since I used Windows & Python but ask yourself if it uses \ instead of / in file paths, also make sure the file is definitely in the location you expect.

3)对于S3上传,Body:是您要上传的实际数据,而不是数据的文件名.您已经在open(...) as file内部调用了它,所以现在有了一个名为file的对象来表示它.

3) For the S3 upload the Body: is the actual data you want to upload, not the filename of the data. You have called it inside open(...) as file so you now have an object called file that represents it.

这篇关于使用boto3将文件上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆