简体   繁体   English

使用 boto3 将文件上传到 S3

[英]Use boto3 to upload a file to 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.我有一个脚本可以将容器中的 csv 文件上传到 S3 存储桶,我将文件复制到本地计算机,并在本地测试脚本,但出现错误。 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,我还在学习一切,试图知道我在脚本中缺少什么部分,以及如何让它运行并将文件上传到 S3,

Here's the errors:这是错误:

error_1:错误_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:错误_02:

File "C:/Users/U12345/IdeaProjects/xxx/s3_upload.py", line 33
    response = s3_client.put_object(Body='C:\Users\U12345/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.另一个问题是我不太确定如何调用这个 function,在括号中放入什么参数,它给了我不同的错误。

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.我已经为此苦苦挣扎了将近一个星期,有点沮丧,有人可以给我一些帮助或一个我可以效仿的好例子。

Update:更新:

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... error02 和最后一个问题已经解决,这只是第一个错误仍然无法正常工作,我正在尝试'/','',有'C:',没有'C:',都不起作用......

I guess you are using put_object() the wrong way.我猜你使用 put_object() 的方式是错误的。 It is used to save an 'object' on s3 & not a file ie you need to first read the file content using pandas.read_csv() or something else & then replace the 'Body' part with the object obtained on reading.Something like this它用于在 s3 上保存“对象”而不是文件,即您需要首先使用 pandas.read_csv() 或其他内容读取文件内容,然后用读取时获得的 object 替换“正文”部分。类似于这个

  df= pandas.read_csv('C:\Users\U12345/IdeaProjects/xxx/test.csv')
  response = s3_client.put_object(Body=df,  
                                    Bucket=output_bucket,
                                    Key='test.csv',
                                    ACL="bucket-owner-full-control")

If you wish to upload the file directly, you should use如果你想直接上传文件,你应该使用

  s3 = boto3.resource('s3')
  s3.meta.client.upload_file('C:\Users\U12345/IdeaProjects/xxx/test.csv', output_bucket, 'test.csv')

You've got a few things to address here so lets break it down a little bit.你有一些事情要在这里解决,所以让我们稍微分解一下。

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. 1) 当您调用upload_to_s3()时,您需要使用已声明的 function 参数、文件名和存储桶密钥来调用它。 So it would be upload_to_s3(filename, bucket_key) for example.所以它会是upload_to_s3(filename, bucket_key)例如。

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. 2)自从我使用 Windows 和 Python 以来已经有一段时间了,但问问自己它是否在文件路径中使用\而不是/ ,还要确保文件绝对位于您期望的位置。

3) For the S3 upload the Body: is the actual data you want to upload, not the filename of the data. 3) 对于 S3 上传Body:是您要上传的实际数据,而不是数据的文件名。 You have called it inside open(...) as file so you now have an object called file that represents it.您已在open(...) as file其称为文件,因此您现在有一个 object 称为file来表示它。

in the last line在最后一行

upload_to_s3()

you haven't actually given the function any parameters.你实际上没有给 function 任何参数。 inside the brackets put in the params:在括号内放入参数:

(source_filename: str, key: str)

ie give the function it's filename and bucket即给 function 它的文件名和存储桶

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM