Skip to content

Instantly share code, notes, and snippets.

@Kibubu
Created October 28, 2022 15:07
Show Gist options
  • Save Kibubu/1881a6e92fa842d04b33590a8468e966 to your computer and use it in GitHub Desktop.
Save Kibubu/1881a6e92fa842d04b33590a8468e966 to your computer and use it in GitHub Desktop.
Script to combine unfinished S3 multipart uploads to one object
#!/bin/bash
# Requirements
# * jq
# * aws cli v1
# Usage
# bash script.sh <bucket>
BUCKET="$1"
# Check if there are pending multipart uploads in the given bucket
MULTIPART_UPLOAD=$(aws s3api list-multipart-uploads --bucket "$BUCKET")
MULTIPART_ID=$(echo "$MULTIPART_UPLOAD" | jq -r '.Uploads[0].UploadId')
MULTIPART_KEY=$(echo "$MULTIPART_UPLOAD" | jq -r '.Uploads[0].Key')
# echo "$MULTIPART_ID"
# echo "$MULTIPART_KEY"
if [ -z "$MULTIPART_ID" ];
then
echo "No multipart uploads found in bucket: $BUCKET"
exit
fi
# If there are pending multipart uploads give the user a choice to list the parts or combine the parts to one object
echo "Found at least on multipart upload in bucket: $BUCKET with key: $MULTIPART_KEY"
echo "Options:"
echo "c: Combine all parts to Object and CompleteMultipartUpload, cannot be reversed"
echo "l: Show parts of multipart upload"
echo "n: Abort and do nothing"
read -r option
case "$option" in
"n")
exit
;;
"l")
# Just list the parts
MULTIPART_UPLOAD_PARTS=$(aws s3api list-parts --bucket "$BUCKET" --upload-id "$MULTIPART_ID" --key "$MULTIPART_KEY")
echo "$MULTIPART_UPLOAD_PARTS"
;;
"c")
# Create argument to pass to `aws` and combine all parts to one object
MULTIPART_UPLOAD_PARTS=$(aws s3api list-parts --bucket "$BUCKET" --upload-id "$MULTIPART_ID" --key "$MULTIPART_KEY")
MULTIPART_UPLOAD_PARTS=$(echo "$MULTIPART_UPLOAD_PARTS" | jq -cr '(.Parts[]| "{ETag=" + (.ETag) + ",PartNumber="+(.PartNumber|tostring) +"}")')
MULTIPART_UPLOAD_PARTS="${MULTIPART_UPLOAD_PARTS//$'\n'/','}"
MULTIPART_UPLOAD_PARTS="Parts=[$MULTIPART_UPLOAD_PARTS]"
echo "$MULTIPART_UPLOAD_PARTS"
aws s3api complete-multipart-upload --bucket "$BUCKET" --key "$MULTIPART_KEY" --upload-id "$MULTIPART_ID" --multipart-upload "$MULTIPART_UPLOAD_PARTS"
;;
*)
exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment