python - How to download the latest file of an S3 bucket using Boto3? -


the other questions find refering older version of boto. download latest file of s3 bucket. in documentation found there method list_object_versions() gets boolean islatest. unfortunately managed set connection , download file. please show me how can extend code latest file of 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 dont know how latest added file bucket called mytestbucket. there various csv files in bucket of course different name.

update:

import boto3 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 = [] file in my_bucket.objects.filter():    unsorted.append(file)  files = [obj.key obj in sorted(unsorted, key=get_last_modified, reverse=true)][0:9] 

this gives me following error:

nameerror: name 'get_last_modified' not defined 

variation of answer provided for: boto3 s3, sort bucket last modified. can modify code suit needs.

get_last_modified = lambda obj: int(obj['lastmodified'].strftime('%s'))  s3 = boto3.client('s3') objs = s3.list_objects_v2(bucket='my_bucket')['contents'] last_added = [obj['key'] obj in sorted(objs, key=get_last_modified)][0] 

if want reverse sort:

[obj['key'] obj in sorted(objs, key=get_last_modified, reverse=true)][0] 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -