Skip to content

Instantly share code, notes, and snippets.

@yairkukielka
Forked from multidis/aws_s3_ls.sh
Created July 27, 2017 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairkukielka/d75e91f8f0380c51933adb989b99f512 to your computer and use it in GitHub Desktop.
Save yairkukielka/d75e91f8f0380c51933adb989b99f512 to your computer and use it in GitHub Desktop.
List of files in a specific AWS S3 location in a shell script.
#!/bin/bash
# setup AWS CLI first
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html
# configure AWS CLI (e.g. use IAM role for S3 access)
export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID=IDHERE
export AWS_SECRET_ACCESS_KEY=KeyHere
# s3 ls command
# http://docs.aws.amazon.com/cli/latest/reference/s3/ls.html
# space-separated string (contains dates etc.)
flist=$(aws s3 ls s3://bucket.name/directory/path/)
# file list as an array
flist=(`aws s3 ls s3://bucket.name/directory/path/ | awk '{print $4}'`)
# first element
echo $flist
# NOTE: indexing starts with 0
echo ${flist[0]}
# all elements
# http://stackoverflow.com/questions/15224535/bash-put-list-files-into-a-variable-and-but-size-of-array-is-1
echo ${flist[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment