79127131

Date: 2024-10-25 19:48:36
Score: 2
Natty:
Report link

Here is the updated code, which provides the expected results. Thanks @AnonCoward for the suggestion.

import boto3
import sys

s3_r = boto3.resource('s3')

bucket_name = 'dummy-data-bucket'
file_key = 'employee_details/emp.csv'

def s3versions(bucket, prefix):
    '''List all versions of files in s3.'''
    bucket = s3_r.Bucket(bucket)
    versions = bucket.object_versions.filter(Prefix = prefix)
    for version in reversed(list(versions)):
        object = version.get()
        version_id = object.get('VersionId')
        if version_id != 'null':      
            path = version.object_key
            last_modified = object.get('LastModified')
            print(path, version_id, last_modified,  sep = '\t')
                
def lambda_handler(event, context):
    s3versions(bucket_name,file_key)
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @AnonCoward
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: sanju