gpt4 book ai didi

python-3.x - 如何检查 boto3 S3.Client.upload_fileobj 是否成功?

转载 作者:行者123 更新时间:2023-12-03 13:20:36 26 4
gpt4 key购买 nike

我想在 S3 上保存长时间运行的作业的结果。这项工作是用 Python 实现的,所以我使用的是 boto3。 user guide说要使用 S3.Client.upload_fileobj 为此目的,效果很好,但我不知道如何检查上传是否成功。根据文档,该方法不会返回任何内容,也不会引发错误。 Callback param 似乎用于进度跟踪而不是错误检查。方法调用是同步还是异步也不清楚。

如果由于任何原因上传失败,我想将内容保存到磁盘并记录错误。所以我的问题是:如何检查 boto3 S3.Client.upload_fileobj 调用是否成功并在失败时进行一些错误处理?

最佳答案

我建议您执行以下操作-

  try:
response = upload_fileobj()
except Exception as e:
save the contents to the disk and log an error.
if response is None:

polling after every 10s to check if the file uploaded successfully or not using **head_object()** function..
If you got the success response from head_object :
break
If you got error in accessing the object:
save the contents to the disk and log an error.

因此,基本上使用 head_object() 进行轮询

关于python-3.x - 如何检查 boto3 S3.Client.upload_fileobj 是否成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com