check if a key exists in a bucket in s3 using boto3

前端 未结 24 2806
花落未央
花落未央 2020-11-28 19:03

I would like to know if a key exists in boto3. I can loop the bucket contents and check the key if it matches.

But that seems longer and an overkill. Boto3 official

相关标签:
24条回答
  • 2020-11-28 19:33
    S3_REGION="eu-central-1"
    bucket="mybucket1"
    name="objectname"
    
    import boto3
    from botocore.client import Config
    client = boto3.client('s3',region_name=S3_REGION,config=Config(signature_version='s3v4'))
    list = client.list_objects_v2(Bucket=bucket,Prefix=name)
    for obj in list.get('Contents', []):
        if obj['Key'] == name: return True
    return False
    
    0 讨论(0)
  • 2020-11-28 19:34

    You can use S3Fs, which is essentially a wrapper around boto3 that exposes typical file-system style operations:

    import s3fs
    s3 = s3fs.S3FileSystem()
    s3.exists('myfile.txt')
    
    0 讨论(0)
  • 2020-11-28 19:35

    For boto3, ObjectSummary can be used to check if an object exists.

    Contains the summary of an object stored in an Amazon S3 bucket. This object doesn't contain contain the object's full metadata or any of its contents

    import boto3
    from botocore.errorfactory import ClientError
    def path_exists(path, bucket_name):
        """Check to see if an object exists on S3"""
        s3 = boto3.resource('s3')
        try:
            s3.ObjectSummary(bucket_name=bucket_name, key=path).load()
        except ClientError as e:
            if e.response['Error']['Code'] == "404":
                return False
            else:
                raise e
        return True
    
    path_exists('path/to/file.html')
    

    In ObjectSummary.load

    Calls s3.Client.head_object to update the attributes of the ObjectSummary resource.

    This shows that you can use ObjectSummary instead of Object if you are planning on not using get(). The load() function does not retrieve the object it only obtains the summary.

    0 讨论(0)
  • 2020-11-28 19:37

    If you seek a key that is equivalent to a directory then you might want this approach

    session = boto3.session.Session()
    resource = session.resource("s3")
    bucket = resource.Bucket('mybucket')
    
    key = 'dir-like-or-file-like-key'
    objects = [o for o in bucket.objects.filter(Prefix=key).limit(1)]    
    has_key = len(objects) > 0
    

    This works for a parent key or a key that equates to file or a key that does not exist. I tried the favored approach above and failed on parent keys.

    0 讨论(0)
  • 2020-11-28 19:42

    Just following the thread, can someone conclude which one is the most efficient way to check if an object exists in S3?

    I think head_object might win as it just checks the metadata which is lighter than the actual object itself

    0 讨论(0)
  • 2020-11-28 19:43

    Not only client but bucket too:

    import boto3
    import botocore
    bucket = boto3.resource('s3', region_name='eu-west-1').Bucket('my-bucket')
    
    try:
      bucket.Object('my-file').get()
    except botocore.exceptions.ClientError as ex:
      if ex.response['Error']['Code'] == 'NoSuchKey':
        print('NoSuchKey')
    
    0 讨论(0)
提交回复
热议问题