Home > Blockchain >  How to list last modified file in S3 using Python
How to list last modified file in S3 using Python

Time:02-16

I'm trying to get the last modified file in S3 using the following:

def lambda_handler(event, context):
    
    import boto3
    
    bucket_name = "arn:aws-us-gov:s3:::some_bucket_name/some_folder/"
    
    get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))

    s3 = boto3.client('s3')
    objs = s3.list_objects_v2(Bucket=bucket_name)['Contents']
    last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified)][0]

...and although things look correct, I keep getting the error:

"errorMessage": "Parameter validation failed:\nInvalid bucket name \"arn:aws-us-gov:s3:::some_bucket_name/some_folder/\": Bucket name must match the regex \"^[a-zA-Z0-9.\\-_]{1,255}$\" or be an ARN matching the regex \"^arn:(aws).*:(s3|s3-object-lambda):[a-z\\-0-9]*:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\\-.]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\\-0-9] :[0-9]{12}:outpost[/:][a-zA-Z0-9\\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\\-]{1,63}$\"",

What am I missing in the bucket name? I copied the ARN from the console and still cannot seem to produce the expected result. I'm not a RegEx guru so it's hard for me to discern what could be missing from the arn. To test, I replaced the bucket name with some random name and got an error that the bucket does not exist; as expected. I'm slightly confused what the issue is. Any assistance would be greatly appreciated.

CodePudding user response:

OK. I've resolved the "issue" and now have what I need.

import boto3
    
bucket_name = "actual_bucket_name"
prefix = "path/to/files/"
    
get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))

s3 = boto3.client('s3')
objs = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter='/' ['Contents']
last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified)][0]

Thank you for the pointers. I was readin through the documentation, however, we know how it can be after staring at walls of text after a while. The "issue" was me not acutely comprehending.

  • Related