如果未下载 s3 文件,则捕获错误
catch errors if s3 file is not downloaded
我像这样从 S3 下载文件:
s3 = boto3.client('s3')
s3.download_file('testunzipping','DataPump_10000838.zip','/tmp/DataPump_10000838.zip')
目前它始终有效。但是,我想添加某种错误处理。如果下载失败,如何检查或获取错误消息。我怎么知道有问题?
boto3 是否提供任何错误处理功能?
我读了这个:Check if S3 downloading finish successfully 但我也在寻找替代品。
你可以有类似下面的东西。下载并确保它已创建。
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
这只是为了改进@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
我像这样从 S3 下载文件:
s3 = boto3.client('s3')
s3.download_file('testunzipping','DataPump_10000838.zip','/tmp/DataPump_10000838.zip')
目前它始终有效。但是,我想添加某种错误处理。如果下载失败,如何检查或获取错误消息。我怎么知道有问题?
boto3 是否提供任何错误处理功能?
我读了这个:Check if S3 downloading finish successfully 但我也在寻找替代品。
你可以有类似下面的东西。下载并确保它已创建。
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
这只是为了改进@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