Like 0

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

download_interactively_from_s3.sh Raw
1#!/bin/bash
2
3target="s3://your-AWS-bucket-here/"
4file=""
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
12while [[ "$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
31done
32
33aws s3 cp $target $file
34
35# do something with $file
36