Skip to content

Instantly share code, notes, and snippets.

@haydenk
Created September 4, 2025 15:18
Show Gist options
  • Select an option

  • Save haydenk/0447754364f21d7b0d81663354035d78 to your computer and use it in GitHub Desktop.

Select an option

Save haydenk/0447754364f21d7b0d81663354035d78 to your computer and use it in GitHub Desktop.
jsonFile="$HOME/files.json"
sourceBucket="your-source-bucket-name"
targetBucket="your-target-bucket-name"
function keyExists {
local searchKey=$1;
aws s3api list-objects-v2 --bucket $targetBucket --query "length(Contents[?ends_with(Key, \`$searchKey\`)])"
}
for object in `jq -cr '.[]' $jsonFile`;
do
key=$(echo $object | jq -cr '.Key')
searchFilename=$(basename $key)
hasKey=$(keyExists $searchFilename)
# echo $searchFilename
if [[ $hasKey -gt 0 ]]; then
continue
fi
lastModified="$(echo $object | jq -cr '.LastModified')"
copySource="$sourceBucket/$key"
tarketKey="usage/$(date -d "$lastModified" '+year=%Y/month=%m/day=%d')/$searchFilename"
echo "$copySource -> $targetBucket/$tarketKey"
aws s3api copy-object --copy-source $copySource --key $tarketKey --bucket $targetBucket | jq .
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment