0

Does anyone have a way in the AWS CLI to get the very LAST object in a bucket by prefix & LastModified date?

I can't seem to find any documentation on how to achieve this. I tried using the below command but it seems to be paginating by n objects, reverse sorts, and outputs that 1 object but repeats for the next batch.

aws --output text s3api list-objects --bucket <bucket-name> --prefix <prefix> --query "reverse(sort_by(Contents,&LastModified))[:1]"
Awatatah
  • 455
  • 1
  • 5
  • 19
  • If you look at, for example, the [Java API](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ListObjectsRequest.html) for listing objects you'll see that the only real filter available is prefix. The APIs's mimic the underlying REST API so it appears that the CLI is doing it the only way it can. One option would be to somehow prefix your objects with date information - it wouldn't be perfect but it might get you closer. – stdunbar Feb 13 '18 at 19:19
  • @stdunbar the problem is that you cannot dynamically set prefixes so if I create e.g. `2018/` prefix, I would have to change it yearly manually because it doesn't accept something like `"Prefix": "%Y/"` – Awatatah Feb 13 '18 at 19:24
  • Possible [duplicate](https://stackoverflow.com/a/47230543/1706504)? – maafk Feb 13 '18 at 21:54
  • @tkwargs no, i have the same issue even with the fix provided in the [link you provided](https://stackoverflow.com/questions/47229999/aws-cli-s3api-find-newest-folder-in-path/47230543#47230543) – Awatatah Feb 13 '18 at 22:04
  • Are you only trying to get a single object? That command you're using should only return a single object, and no pagination should be involve. Perhaps I'm missing your question – maafk Feb 14 '18 at 13:04
  • @tkwargs for some reason this is what is happening, – Awatatah Feb 20 '18 at 15:37

0 Answers0