How to capture botocore's NoSuchKey exception?

后端 未结 4 1395
时光说笑
时光说笑 2021-01-30 15:37

I\'m trying to write \"good\" python and capture a S3 no such key error with this:

session = botocore.session.get_session()
client = session.create_client(\'s3\'         


        
4条回答
  •  终归单人心
    2021-01-30 15:58

    In boto3, I was able to access the exception in resource's meta client.

    import boto3
    
    s3 = boto3.resource('s3')
    s3_object = s3.Object(bucket_name, key)
    
    try:
        content = s3_object.get()['Body'].read().decode('utf-8')
    except s3.meta.client.exceptions.NoSuchKey:
        print("no such key in bucket")
    

提交回复
热议问题