check if file exists on s3 python

Solutions on MaxInterview for check if file exists on s3 python by the best coders in the world

showing results for - "check if file exists on s3 python"
Anna
27 Jun 2020
1import boto3
2
3def get_resource(config: dict={}):
4    """Loads the s3 resource.
5
6    Expects AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be in the environment
7    or in a config dictionary.
8    Looks in the environment first."""
9
10    s3 = boto3.resource('s3',
11                        aws_access_key_id=os.environ.get(
12                            "AWS_ACCESS_KEY_ID", config.get("AWS_ACCESS_KEY_ID")),
13                        aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", config.get("AWS_SECRET_ACCESS_KEY")))
14    return s3
15
16
17def get_bucket(s3, s3_uri: str):
18    """Get the bucket from the resource.
19    A thin wrapper, use with caution.
20
21    Example usage:
22
23    >> bucket = get_bucket(get_resource(), s3_uri_prod)"""
24    return s3.Bucket(s3_uri)
25
26
27def isfile_s3(bucket, key: str) -> bool:
28    """Returns T/F whether the file exists."""
29    objs = list(bucket.objects.filter(Prefix=key))
30    return len(objs) == 1 and objs[0].key == key
31
32
33def isdir_s3(bucket, key: str) -> bool:
34    """Returns T/F whether the directory exists."""
35    objs = list(bucket.objects.filter(Prefix=key))
36    return len(objs) > 1
37