Source code for s3pathlib.better_client.head_bucket
# -*- coding: utf-8 -*-"""Improve the head_bucket_ API... _head_bucket: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.head_bucket"""importtypingasTimportbotocore.exceptionsifT.TYPE_CHECKING:# pragma: no coverfrommypy_boto3_s3importS3Client
[docs]defis_bucket_exists(s3_client:"S3Client",bucket:str,)->bool:""" Check if a bucket exists. Example:: >>> is_bucket_exists(s3_client, "my-bucket") True :param s3_client: See head_bucket_ :param bucket: See head_bucket_ :return: A Boolean flag to indicate whether the bucket exists. """try:s3_client.head_bucket(Bucket=bucket)returnTrueexceptbotocore.exceptions.ClientErrorase:if"Not Found"instr(e):returnFalseelse:# pragma: no coverraisee