Skip to content

Instantly share code, notes, and snippets.

@fbarriga
Last active November 5, 2023 17:09
Show Gist options
  • Save fbarriga/9e6b47e425422612862518cbd3a53b82 to your computer and use it in GitHub Desktop.
Save fbarriga/9e6b47e425422612862518cbd3a53b82 to your computer and use it in GitHub Desktop.
S3: Dump keys from AWS S3 Bucket with pagination using bash

Dump keys from AWS S3 Bucket with pagination

$ export url='https://BUCKET_NAME.s3.amazonaws.com/?list-type=2'
$ function dump_urls { cat data.xml \
  | sed 's/<Contents>/\n/g' \
  | sed -ne 's/.*<Key>\(.*\)<\/Key>.*/\1/p' >> urls.txt; \
  }
$ rm -f urls.txt data.xml
$ curl $url -o data.xml
$ dump_urls
$ while (grep "<IsTruncated>true</IsTruncated>" data.xml > /dev/null;) do \
  curl "$url&start-after=$(tail -n1 urls.txt)" -o data.xml; \
  dump_urls; \
done

The urls are dumped to urls.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment