bash, issue on listing amazon s3 element -


i'm trying list , echo files having yesterday's date in names, on amazon s3 bucket.

i know can do:

s3cmd ls s3://my-bucket/`date +%y%m%d -d "1 day ago"`* 

which working fine, need several stuff on these files, 1 bye one.

so code is:

for file in $(s3cmd ls s3://my-bucket/$(date +%y%m%d -d "1 day ago")*) echo $file done 

it works fine instead of retrieve me file name give me every single element: date uploaded , weight:

s3://my-bucket/20150720-1437436434_ip-10-0-1-36_android.log.gz 2015-07-20 23:55 6180965 

how can retrieve file's name? ie: s3://my-bucket/20150720-1437436434_ip-10-0-1-36_android.log.gz

rather using s3cmd, these days recommend use official aws command-line interface (cli). has great features such --query (to control output) , access every aws api call.

here's sample fit need:

for file in $(aws s3api list-objects --bucket bucket-name --prefix `date +%y%m%d -d "1 day ago"` --query 'contents[*].key' --output text); echo $file; done 

breaking down:

  • aws s3api list-objects outputs information amazon s3 object
  • --bucket specifies amazon s3 bucket
  • --prefix specifies start of amazon s3 object name (which works use-case)
  • --query describes output fields required (here object key, filename)
  • --output 1 of json, text, table

see: list-objects cli documentation

as simpler version of listing objects available (aws s3 ls bucket-name) has fixed text output, s3cmd.


Comments