Skip to content

Commit

Permalink
ccmlib/common: improve aws_bucket_ls
Browse files Browse the repository at this point in the history
Automaticlly sort by date the returned file list, since the users
of this function only get the key names, and loses the modify date
and parsing the date out of the name isn't trivial
  • Loading branch information
fruch committed Jul 9, 2023
1 parent e721a5b commit ad16596
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions ccmlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,21 +943,16 @@ def assert_jdk_valid_for_cassandra_version(cassandra_version):
exit(1)


def aws_bucket_ls(s3_url):
def aws_bucket_ls(s3_url: str) -> list[str]:
bucket_object = s3_url.replace('https://s3.amazonaws.com/', '').split('/')
prefix = '/'.join(bucket_object[1:])
s3_conn = Session().client(service_name='s3', config=Config(signature_version=UNSIGNED))
paginator = s3_conn.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket=bucket_object[0], Prefix=prefix)

files_in_bucket = []
for page in pages:
if 'Contents' not in page:
break
s3_resource = Session().resource(service_name='s3', config=Config(signature_version=UNSIGNED))
bucket = s3_resource.Bucket(bucket_object[0])

files_in_bucket = bucket.objects.filter(Prefix=prefix)

for obj in page['Contents']:
files_in_bucket.append(obj['Key'].replace(prefix + "/", ''))
return files_in_bucket
return [f.key.replace(prefix + "/", '') for f in sorted(files_in_bucket, key=lambda x: x.last_modified)]


def grouper(n, iterable, padvalue=None):
Expand Down

0 comments on commit ad16596

Please sign in to comment.