Guest User

Untitled

a guest
Jun 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. #
  4. # This file will filter all s3 objects where the key/path contains a specific pattern and apply the provided tag.
  5. # Results are split into X number of backfground processes, each tagging 1000 objects
  6. #
  7.  
  8.  
  9. BUCKET_NAME="YOURBUCKET"
  10. function tagObjects {
  11. type=$1
  12. pattern=$2
  13. tagging=$3
  14.  
  15. # get listing, save to file
  16. if [ ! -e objects-${type}.txt ];then
  17. aws s3api list-objects-v2 \
  18. --bucket ${BUCKET_NAME} \
  19. --prefix artifacts \
  20. --query "Contents[?contains(Key,${pattern})].[Key]" \
  21. --output text > objects-$type.txt
  22. fi
  23. echo "Got objects"
  24.  
  25. # split file if >1000 to run parallel
  26. mkdir splitfiles-$type
  27. cd splitfiles-$type
  28. split -l 1000 ../objects-$type.txt
  29. cd ..
  30.  
  31. mkdir logs-$type
  32. function tag_objects {
  33. logpath=logs-$type/$(basename ${1}).log
  34. while read key;do
  35. echo "Tagging ${key}" >> $logpath
  36. aws s3api put-object-tagging --bucket ${BUCKET_NAME} --key ${key} --tagging ${tagging} >> $logpath
  37. done < $1
  38. echo "Done file $1"
  39. }
  40. for file in splitfiles-${type}/*;do
  41. echo "Sending file ${file} o background"
  42. tag_objects ${file} &
  43. done
  44.  
  45. wait
  46. }
  47.  
  48. tagObjects "cache" "'global/cache'" "TagSet=[{Key=circleci.objectType,Value=project.cache}]"
  49. tagObjects "workspaces" "'workflows/workspaces'" "TagSet=[{Key=circleci.objectType,Value=workflow.workspace}]"
  50. tagObjects "artifact" "'/artifact/'" "TagSet=[{Key=circleci.objectType,Value=job.artifact}]"
Add Comment
Please, Sign In to add comment