The other questions I could find were refering to an older version of Boto. I would like to download the latest file of an S3 bucket. In the documentation I found that there is a method list_object_versions() that gets you a boolean IsLatest. Unfortunately I only managed to set up a connection and to download a file. Could you please show me how I can extend my code to get the latest file of the bucket? Thank you
import boto3
conn = boto3.client('s3',
region_name="eu-west-1",
endpoint_url="customendpoint",
config=Config(signature_version="s3", s3={'addressing_style': 'path'}))
From here I dont know how to get the latest added file from a bucket called mytestbucket
. There are various csv files in the bucket but all of course with a different name.
Update:
import boto3
from botocore.client import Config
s3 = boto3.resource('s3', region_name="eu-west-1", endpoint_url="custom endpoint", aws_access_key_id = '1234', aws_secret_access_key = '1234', config=Config(signature_version="s3", s3={'addressing_style': 'path'}))
my_bucket = s3.Bucket('mytestbucket22')
unsorted = []
for file in my_bucket.objects.filter():
unsorted.append(file)
files = [obj.key for obj in sorted(unsorted, key=get_last_modified, reverse=True)][0:9]
This gives me the following error:
NameError: name 'get_last_modified' is not defined