skip to Main Content

I am using the following syntax to list versions in s3 using boto3.

object_versions = s3.list_object_versions(Bucket=bucket_name)

Strangely, it is not listing the objects with only one version left. Please see the screenshot below, i can see the objects on the aws console but i can’t get them from my boto3 script.

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I found the problem. The list object api of boto3 only lists limited results (~1000 objects). To go beyond you may need to use paginator.

    Here is a sample code, you can ignore the unwanted lines.

    import boto3
    
    
    s3 = boto3.client('s3')
    
    paginator = s3.get_paginator('list_object_versions')
    pages = paginator.paginate(Bucket='bucket_name', Prefix='')
    
    for page in pages:
        for obj in page['Versions']:
                print(obj['Key'])
        
    

  2. import boto3
    
    # Create an S3 client
    s3 = boto3.client('s3')
    
    # Specify the bucket name
    bucket_name = 'your_bucket_name'
    
    # List all versions of objects in the bucket
    object_versions = s3.list_object_versions(Bucket=bucket_name)
    
    # Print information about each version (including delete markers)
    for version in object_versions.get('Versions', []):
        print(f"Key: {version['Key']}, VersionId: {version['VersionId']}, IsDeleteMarker: False")
    
    # Print information about each delete marker (if any)
    for delete_marker in object_versions.get('DeleteMarkers', []):
        print(f"Key: {delete_marker['Key']}, VersionId: {delete_marker['VersionId']}, IsDeleteMarker: True")
    

    This code will print information about all versions, including both versioned objects and their delete markers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search