Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the most recent file in cloud storage bucket?

Is this something that can be done with gsutil?

https://cloud.google.com/storage/docs/gsutil/commands/ls does not seem to mention any sorting functionality - only filtering by a date - which wouldn't work for my use case.

like image 756
Chris Stryczynski Avatar asked Aug 18 '17 16:08

Chris Stryczynski


2 Answers

Hello this still doesn't seems to exists, but there is a solution in this post: enter link description here

The command used is this one:

gsutil ls -l gs://[bucket-name]/ | sort -k 2

As it allow you to filter by date you can get the most recent result in the bucket and recuperating the last line using another pipe if you need.

like image 167
night-gold Avatar answered Sep 16 '22 12:09

night-gold


gsutil ls -l gs://<bucket-name> | sort -k 2 | tail -n 2 | head -1 | cut -d ' ' -f 7

It will not work well if there is less then two objects in the bucket though

like image 20
flowfleeflop Avatar answered Sep 16 '22 12:09

flowfleeflop