Skip to content

Instantly share code, notes, and snippets.

@jdavidbakr
Created March 21, 2022 13:38
Show Gist options
  • Save jdavidbakr/7ca2ff3ad005098c0a2febb1eec7d691 to your computer and use it in GitHub Desktop.
Save jdavidbakr/7ca2ff3ad005098c0a2febb1eec7d691 to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage
# ./fix-pdf-s3.sh bucket path/to/folder/to/scan
# No trailing slash on the bucket or path
bucket=$1
path=$2
origin=$1
fixfile() {
echo "Fixing file $file"
aws s3 cp --content-type "application/pdf" --metadata-directive REPLACE s3://$bucket/$path/$file s3://$bucket/$path/$file
}
checkpdffile() {
fileInfo=`aws s3api get-object --bucket $bucket --key $path/$file /dev/null`
mime=`echo $fileInfo | python3 -c "import sys, json; print(json.load(sys.stdin)['ContentType'])"`
if [ $mime != "application/pdf" ]
then
fixfile
else
echo $file is the correct mime type.
fi
}
checkfile() {
if [[ $file == *.pdf ]]
then
checkpdffile
fi
}
for file in $(aws s3 ls $bucket/$path/ | awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//');
do
checkfile
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment