我從 S3 下載一個檔案,如下所示:
s3 = boto3.client('s3')
s3.download_file('testunzipping','DataPump_10000838.zip','/tmp/DataPump_10000838.zip')
現在它總是有效。但是,我想添加某種錯誤處理。如果下載失敗,我如何檢查或收到錯誤訊息。我怎么知道有問題?
boto3 是否提供任何錯誤處理功能?
我讀到:檢查 S3 下載是否成功完成,但我也在尋找替代方案。
uj5u.com熱心網友回復:
你可以有類似下面的東西。下載并確保它已創建。
import boto3
import os
def download_and_verify(Bucket, Key, Filename):
try:
os.remove(Filename)
s3 = boto3.client('s3')
s3.download_file(Bucket,Key,Filename)
return os.path.exists(Filename)
except Exception: # should narrow the scope of the exception
return False
uj5u.com熱心網友回復:
這只是為了改進@balderman 的答案,實際檢查導致您的 BOTO 請求失敗的例外。
def download_and_verify(Bucket, Key, Filename):
try:
os.remove(Filename)
s3 = boto3.client('s3')
s3.download_file(Bucket,Key,Filename)
return os.path.exists(Filename)
except botocore.exceptions.ClientError as error:
print(error.response['Error']['Code']) #a summary of what went wrong
print(error.response['Error']['Message']) #explanation of what went wrong
return False
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331391.html
標籤:Python 蟒蛇-3.x 亚马逊网络服务 亚马逊-s3 博托
