Skip to content

Instantly share code, notes, and snippets.

@kshivang
Last active December 12, 2017 11:02
Show Gist options
  • Save kshivang/a8c5e1bd8ca8988e738bcd3a2c29e5c3 to your computer and use it in GitHub Desktop.
Save kshivang/a8c5e1bd8ca8988e738bcd3a2c29e5c3 to your computer and use it in GitHub Desktop.
Amazon S3 storage and Firebase Database indexing
#!/bin/bash
# Please pay attention to comment till line 12
# These are your configuration variables
# Configuration
SERVER="Amazon server where S3 is hosted" # "s3.ap-south-1"
BUCKET_NAME="S3 Bucket Name" #Bucket Name
FOLDER_PATH="/default/S3/FOLDER" # Default S3 Folder to upload
FIREBASE_URL="https://your-project.firebaseio.com" #Your firebase project URL
# Configuration End
MODE="file"
FILE=""
FOLDER=()
function getFirebaseURL {
URL=$FIREBASE_URL
for folder in "${FOLDER[@]}"
do :
URL=$URL/$folder
done
if [ $MODE == "folder" ]; then
URL="$URL/$FILE"
fi
URL=$URL.json
echo $URL
}
function parseFileJSON {
FILE_URL="https://${SERVER}.amazonaws.com/${BUCKET_NAME}${FOLDER_PATH}/$FILE"
file="${FILE%.*}"
JSON="{ \"${file}\": \"${FILE_URL}\" }"
echo $JSON
}
function parseFolderJSON {
cd ${FILE}
files=(*)
i=0
for file in "${files[@]}"
do
:
FILE_URL="https://${SERVER}.amazonaws.com/${BUCKET_NAME}${FOLDER_PATH}/$FILE/$file"
file_name="${file%.*}"
if [ $i -eq 0 ]; then
JSON="{ \"${file_name}\": \"${FILE_URL}\" "
else
JSON="${JSON}, \"${file_name}\": \"${FILE_URL}\" "
fi
i=$(($i+1))
done
JSON="${JSON} }"
echo $JSON
}
function updateToFirebase {
URL=$(getFirebaseURL)
echo "Firebase Patch request at $URL"
if [ ${MODE} == "file" ]; then
JSON=$(parseFileJSON)
echo $JSON > upload.json
curl -X PATCH -d @upload.json $URL
else
JSON=$(parseFolderJSON)
echo $JSON > upload.json
curl -X PATCH -d @upload.json $URL
fi
rm upload.json
}
function printHelp {
echo "Usage: "
echo " Just use af fileName or directoryName"
echo " -h <help> print help(this message)"
echo " -b <batch files> take batch upload"
echo " -f <file> file/folder to upload"
echo " -d <directory> aws directory where to upload e.g /sound/davidji"
echo " -m for meditation directory"
echo " -a for asmr directory"
}
FOLDER=${setFolder}
if [[ "$#" -eq 0 ]]; then
echo "Error: You need to pass to pass arguments"
printHelp
exit 1
fi
while getopts "h:b:f:d" opt; do
case "$opt" in
h)
printHelp
exit 0
;;
b)
if [[ -d $OPTARG ]]; then
FILE=$OPTARG
MODE="folder"
if [[ $FILE == */ ]]; then
FILE=${FILE%?}
fi
else
echo "Error: $OPTARG is not a diretory."
printHelp
exit 1
fi
;;
f)
if [[ -d $OPTARG ]]; then
FILE=$OPTARG
else
echo "Error: $OPTARG is not a file."
printHelp
exit 1
fi
;;
d)
FOLDER_PATH=$OPTARG
FOLDER=$(setFolder)
;;
m)
FOLDER_PATH="/sound/meditation"
FOLDER=$(setFolder)
;;
a)
FOLDER_PATH="/sound/asmr"
FOLDER=$(setFolder)
;;
*)
FILE=$OPTARG
if [[ -d $FILE ]]; then
echo "Identified given argument as directory."
MODE="folder"
if [[ $FILE == */ ]]; then
FILE=${FILE%?}
fi
elif [[ -f $FILE ]]; then
echo "Identified given argument as file."
else
echo "Error: Given argument $FILE is not valid file or directory"
printHelp
exit 1
fi
esac
done
FOLDER=(${FOLDER_PATH//\// })
S3_URI="s3://${BUCKET_NAME}${FOLDER_PATH}"
if [ "${MODE}" == "file" ]; then
aws s3 cp ${FILE} ${S3_URI}/${FILE}
else
aws s3 cp ${FILE} ${S3_URI}/${FILE} --recursive
fi
updateToFirebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment