Pages

Wednesday, August 19, 2020

AWS: Extract a ZIP from one bucket to another bucket

 Below is the Script which help me to Extract the ZIP from S3 Bucket from one AWS Account to another S3 account.

This particular Script will do the following things,

  • List the Files on S3 Bucket and find the latest Build/file,
  • Download the ZIP file form S3 Bucket of different account,
  • Extract the ZIP and update the server-config.json,
  • Replace the string with SED command,
  • Upload the Extracted folder to different S3 bucket. 

# Download the AdminPortal zip

pwd

export ZIPNAME=`aws s3 ls ${S3_Source_Bucket} --profile=apportal-dev | grep AdminPortal | cut -d" " -f7 | tail -1`

echo ${ZIPNAME}

export ZIPVER=`echo ${ZIPNAME} | cut -d"." -f2-5`


Download_S3 ()

{

  echo "================================================================="

  echo "Downloading the UI Zip from APP-Deliveries Bucket"

  echo "================================================================="

  #aws s3 cp s3://s3-app-dev-deliveries/AdminPortal.1.0.0.1.zip .

  aws s3 cp s3://${S3_Source_Bucket}/${ZIPNAME} .


  echo "================================================================="

  echo "uncompressing the Zip"

  echo "================================================================="

  unzip ${ZIPNAME}

  

  echo "================================================================="

  echo "Updating the URLs"

  echo "================================================================="

  cd ap-ap

  cp server-config.json server-config.json.bkp

  cp server-config.json temp-file.txt


   OLD_AAS=AASSERVER

   NEW_AAS=${AASSERVER}

   OLD_LAMB=LAMBDASERVER

   NEW_LAMB=${LAMBDASERVER}

   OLD_SSP=SSPSERVER

   NEW_SSP=${SSPSERVER}

   

   sed -i 's|'"$OLD_AAS"'|'$NEW_AAS'|g' temp-file.txt

   sed -i 's|'"$OLD_LAMB"'|'$NEW_LAMB'|g' temp-file.txt

   sed -i 's|'"$OLD_SSP"'|'$NEW_SSP'|g' temp-file.txt


   #perl -i -p -e 's/AASSERVER/$AASSERVER/g' temp-file.txt

   #perl -i -p -e 's/LAMBDASERVER/$LAMBDASERVER/g' temp-file.txt

 

  mv temp-file.txt server-config.json

  cat server-config.json

  echo "https://s3-app-dev-deliveries.s3.amazonaws.com/${ZIPNAME}" > version-${ZIPVER}.txt

}


Upload_S3 ()

{

  echo "================================================================="

  echo "Uploading the Static Website to the UI Bucket"

  echo "================================================================="

  cd ..

  #aws s3 cp ap-ap s3://${S3_Dest_Bucket} --recursive

  aws s3 sync ap-ap s3://${S3_Dest_Bucket} --delete --exclude '*.svg'

  aws s3 sync ap-ap s3://${S3_Dest_Bucket} --delete --exclude '*' --include '*.svg' --content-type 'image/svg+xml'

}

Empty_S3 ()

{

 aws s3 rm s3://${S3_Dest_Bucket} --recursive

 echo "${S3_Dest_Bucket} Bucket is emptied"

}


Update_ver ()

{

 cd ${WORKSPACE}/ap-ap

 cp /var/opt/hid/app-version/background.jpg /var/opt/hid/app-version/version.html .

  sed -i 's|'PLAT'|'${ENV}'|g' version.html

  sed -i 's|'UPDATEVER'|'${ZIPVER}'|g' version.html

}


if [ "$EMPTY_BUCKET" == "yes" ]; then

    echo "Empty the S3 and Uploading the new Static Website."

    Empty_S3

    Download_S3

    Update_ver

    Upload_S3

else

   echo "Uploading to S3 Without Clearing S3"

   Download_S3

   Update_ver

   Upload_S3

fi

No comments:

Post a Comment