Like 0

How to loop through folders in AWS S3, select a file, and download it

Will Power revised this gist 6 years ago · 3f2557b

1 file changed, 35 insertions

download_interactively_from_s3.sh (file created)
@@ -0,0 +1,35 @@
1 + #!/bin/bash
2 +
3 + target="s3://your-AWS-bucket-here/"
4 + file=""
5 +
6 + # if you're looking for a specific file extension
7 + # you can do something like this instead
8 + # while [[ "$target" != *.tgz ]]; do
9 +
10 + # while there is a slash at the end of the path
11 + # i.e. it isn't a file
12 + while [[ "$target" == */ ]]; do
13 + list=$(aws s3 ls "$target")
14 +
15 + # create an array of options using the
16 + # last "word" in each line of the
17 + # output from aws
18 + options=()
19 + while IFS= read; do
20 + options+=( $(echo "$REPLY" | awk '{print $(NF)}') )
21 + done <<< "$list"
22 +
23 + # select an option and append it
24 + select option in "${options[@]}"; do
25 + [ -z "$option" ] && exit 1
26 +
27 + target="$target$option"
28 + file="$option"
29 + break
30 + done
31 + done
32 +
33 + aws s3 cp $target $file
34 +
35 + # do something with $file