0% found this document useful (0 votes)
14 views6 pages

S3Operations 3.Sh

Uploaded by

shruti.tranquil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

S3Operations 3.Sh

Uploaded by

shruti.tranquil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#!

/bin/bash
# Color Code and Name
NC='\033[0m' # No Color
Light_Red='\033[1;31m'
Green='\033[0;32m'
Light_Green='\033[1;32m'
Yellow='\033[1;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Light_Purple='\033[1;36m'

function operation_completed() {
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
printf "${Green}S3 file upload completed successfully ! ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
}

function print_by_message() {
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
printf "${Green}${1} ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
}

function operation_failed() {
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
printf "${Light_Red}${1} ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
}

function helpFunction() {
echo ""
echo "Usage: $0 -f filePath -ca checksumAlgo -t tags -i fileId"
echo -e "\t-f Valid file path to be uploaded to s3 bucket"
echo -e "\t-a Checksum algorithm, allowed value: one of
[CRC32,CRC32C,SHA1,SHA256]"
echo -e "\t-t Tags as valid json, example -t
'[{\"tagName\":\"companyCode\",\"tagValue\":\"4\"},
{\"tagName\":\"acquisitionChannel\",\"tagValue\":\"acq\"},
{\"tagName\":\"fileFormat\",\"tagValue\":\"pain00100109\"},
{\"tagName\":\"customerReference\",\"tagValue\":\"12345\"}]'"
echo -e "\t-i file id to be downloaded from s3 bucket"
exit 1 # Exit script after printing help
}

function getCompanyCode() {
namespace=$1
companyCode=""
case $namespace in
pm2intnl | pm2asbnl | dcaccnl) companyCode="2" ;;
pm2intuk) companyCode="5" ;;
pm2intbe | pm2asbbe | dcaccbe | dcmain02) companyCode="1" ;;
**) companyCode="4" ;;
esac
echo "$companyCode"
}

printf "\n$
{Yellow}***************************************************************************
*****${NC}\n"
printf "${Light_Green}S3 file operation ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n\n"

printf "\n"

while getopts "f:a:t:i:" opt; do


case "$opt" in
f) filePath="$OPTARG" ;;
a) checksumAlgo="$OPTARG" ;;
t) tags="$OPTARG" ;;
i) fileId="$OPTARG" ;;
?) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
done

if [[ -z $filePath && -z $fileId ]]; then


operation_failed "Please provide either a valid file path with -f option or file
id with -i option"
exit 1
fi

if [[ -n "$filePath" && -n "$fileId" ]]; then


operation_failed "Both operations can not be performed simultaneously, Please
provide either a valid file path with -f option or file id with -i option"
exit 1
fi

if [ -n "$filePath" ]; then
file="$filePath"
echo "File passed from shell script : $file"
printf "\n"

if [[ ! -f $file ]]; then


operation_failed "$file does not exist"
exit 1
fi
if [[ -n "$checksumAlgo" ]]; then
case $checksumAlgo in
CRC32 | CRC32C | SHA1 | SHA256) ;;
*)
operation_failed "Invalid algorithm provided \"$checksumAlgo\"! Authorized
value is one of [CRC32,CRC32C,SHA1,SHA256]"
exit 1
;;
esac
else
checksumAlgo=CRC32C
fi
if [[ -n $tags ]]; then
if ! jq -e . >/dev/null 2>&1 <<<$tags; then
operation_failed "could not parse tag information, It has to be valid json
of following format - '[{\"tagName\":\"companyCode\",\"tagValue\":\"4\"},
{\"tagName\":\"acquisitionChannel\",\"tagValue\":\"acq\"},
{\"tagName\":\"fileFormat\",\"tagValue\":\"pain00100109\"},
{\"tagName\":\"customerReference\",\"tagValue\":\"12345\"}]'"
exit 1
fi
fi
printf "\n"
fi

PS3=$'\nPlease select the right namespace[Provide the input as number] : '


nsArr=("pom" "pom-dev" "rtpm-dev" "pm2intnl" "pm2intbe" "pm2intuk" "pm2asbnl"
"pm2asbbe" "pm2asbuk" "dcaccnl" "dcaccbe", "dcmain02")
selectedNs=""
selectedProfile=""
select choosen in "${nsArr[@]}"; do
if [[ ! "${nsArr[*]}" =~ $choosen ]]; then
printf "$
{Yellow}***************************************************************************
******${NC}\n"
printf "${Light_Red}Invalid value ! Please select valid option. ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
continue
fi
selectedNs="$choosen"
selectedProfile="$choosen"
printf "\n${Green}You have selected option for : $selectedNs ${NC}\n \n"
break
done

printf "\n"

bucket=""

if [ -n "$filePath" ]; then
PS3=$'\nPlease select the right mode[Provide the input as number] : '
modeArr=("hybrid" "sbcp")
selectedMode=""
select choosenMode in "${modeArr[@]}"; do
if [[ ! ${modeArr[*]} =~ $choosenMode ]]; then
printf "$
{Yellow}***************************************************************************
******${NC}\n"
printf "${Light_Red}Invalid value ! Please select valid option. ${NC}\n"
printf "$
{Yellow}***************************************************************************
*****${NC}\n"
continue
fi
selectedMode="$choosenMode"
printf "\n${Green}You have selected option for : $selectedMode ${NC}\n \n"
break
done
fi

awsResult=""
if [[ "${selectedMode}" = "sbcp" || -z $selectedMode ]]; then
environment="pm2dev2"
domain="sbcp.io"
companyCode=$(getCompanyCode "$selectedNs")
tokenEndPointAuth=""
if [ "${selectedNs}" == "dcaccnl" ] || [ "${selectedNs}" == "dcaccbe" ]; then
environment="dcrdqaew1"
domain="soprabanking.cloud"
fi
loginURL="https://login.$selectedNs.$environment.$domain"
case $selectedNs in
pm2intnl | pm2asbnl | pm2intuk | pm2intbe | pm2asbbe | pm2asbuk | pm2asbbe |
dcaccnl | dcaccbe | dcmain02)
intTokenURL="https://api.$selectedNs.$environment.$domain/core/authentication/
external/v1/private/token"

tokenEndPointAuth="dGVzdC10ZW5hbnQtaW50ZXJuYWwtYXBwOnRlc3QtdGVuYW50LWludGVybmFsLWFw
cC1zZWNyZXQ="
;;
*)
intTokenURL="$loginURL/reactive-server-external-auth/private/oauth/token"

tokenEndPointAuth="a2Fma2EtcmVzdC1wcm94eS1lbmNyeXB0ZWQtaW50ZXJuYWwtYXBwOlEua2IsPHcr
PSN9aCFZeVQ8LHJgamUpdiRTOm4nXQ=="
;;
esac
internalToken=$(curl --location -s --request POST "$intTokenURL" \
--header "Authorization: Basic $tokenEndPointAuth" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'scope=default-scope' \
--data-urlencode 'token_type=TENANT_INTERNAL_APP' \
--data-urlencode 'grant_type=client_credentials' | jq -r '.access_token')
if [ ! -z "$filePath" ]; then
if [ -n "$tags" ]; then
request_body='{
"name": "'"$file"'",
"tags": '$tags'
}'
else
request_body='{
"name": "'"$file"'"
}'
fi
result=$(curl -s --location --request POST "$loginURL/service-pom-payment-
order-bulk-processing/v1/initiated-payment-orders-file" \
--header "X-SBS-Current-Company: $companyCode" \
--header "Authorization: Bearer $internalToken" \
--header 'Content-Type: application/json' \
--data-raw "$request_body")
else
result=$(curl -s --location --request GET "$loginURL/service-pom-payment-order-
bulk-processing/v1/initiated-payment-orders-file-report/$fileId" \
--header "X-SBS-Current-Company: $companyCode" \
--header "Authorization: Bearer $internalToken" \
--header 'Content-Type: application/json'
)
fi

echo "$result" >result.json


accessKeyId=$(cat result.json | jq -r '.credentials.accessKeyId')
secretAccessKey=$(cat result.json | jq -r '.credentials.secretAccessKey')
sessionToken=$(cat result.json | jq -r '.credentials.sessionToken')

if [ "$accessKeyId" == "null" ]; then


errorLabel=$(cat result.json | jq -r '.feedbacks[0].label')
errorCode=$(cat result.json | jq -r '.feedbacks[0].code')
operation_failed "Error occurred while fetching credentials from sflm. Error
code: $errorCode, Error label: $errorLabel"
exit 1
else
bucket=$(cat result.json | jq -r '.storedFile.bucket')
prefix=$(cat result.json | jq -r '.storedFile.prefix')
id=$(cat result.json | jq -r '.storedFile.id')
filename=$(cat result.json | jq -r '.storedFile.name')
externalTransferUrl=$(cat result.json | jq -r '.externalTransferUrl')

aws configure set aws_access_key_id "$accessKeyId"


aws configure set aws_secret_access_key "$secretAccessKey"
aws configure set aws_session_token "$sessionToken"

echo "accessKeyId: $(aws configure get aws_access_key_id)"


echo "secretAccessKey: $(aws configure get aws_secret_access_key)"
echo "sessionToken: $(aws configure get aws_session_token)"

echo "bucket: $bucket"


echo "prefix: $prefix"
echo "id: $id"
echo "filename: $filename"
echo "externalTransferUrl: $externalTransferUrl"

printf "\n"

if [ -n "$filePath" ]; then
awsResult=$(aws --endpoint-url $externalTransferUrl s3 cp $filename
s3://$bucket/$prefix/$id)
commandResult=$?
printf "\n"
if [ $commandResult -eq 0 ]; then
echo "awsResult : $awsResult"
print_by_message "File successfully uploaded in S3 bucket!"
fi
else
awsResult=$(aws --endpoint-url "$externalTransferUrl" s3 cp
"s3://$bucket/$prefix/$id" "${filename}.xml")
commandResult=$?
printf "\n"
if [ $commandResult -eq 0 ]; then
echo "awsResult : $awsResult"
print_by_message "File successfully downloaded from S3 bucket!"
fi
fi
fi
else
if [ "${selectedNs}" = "dcaccnl" ] || [ "${selectedNs}" = "dcaccbe" ]; then
bucket="sbcp-dcrdqaew1-file-mgmt"
else
bucket="sbcp-pm2dev2-file-mgmt"
fi
prefix="${selectedNs}/other/pom-create-initiated-payment-orders-file-sftp"
filename=$(basename "$file")
externalTransferUrl="https://s3.eu-west-1.amazonaws.com"

echo "bucket: $bucket"


echo "filename: $filename"
echo "externalTransferUrl: $externalTransferUrl"
echo "checksum-algorithm: $checksumAlgo"
echo "prefix: $prefix"
awsResult=$(aws --endpoint-url $externalTransferUrl s3api put-object --bucket
$bucket --key $prefix/$filename --profile $selectedProfile --region eu-west-1 --
body $file)
commandResult=$?
printf "\n"

if [ $commandResult -eq 0 ]; then


echo "awsResult : $awsResult"
print_by_message "File successfully uploaded in S3 bucket!"
fi
fi

You might also like