Skip to content

Commit a136101

Browse files
author
Hasshim Ali
committed
Updated multipart_upload.sh
1 parent 44dd4ce commit a136101

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# scripts
2-
my scripts
2+
My personal scripts. Feel free to download, edit and use them.

aws_s3_multipart_upload.sh

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
#!/bin/bash
2+
# Created by Hasshim Ali
3+
# Version 1.0
24
# This script is used to split files and upload them to S3 using the multipart upload.
35
# 1. Execute the script, set the number of files to be splitted.
46
# 2. Provide the filename.
7+
############################################################################
58

69
split_upload () {
7-
# split the file into chunks
8-
echo "Spliiting file...."
10+
# Split the file into chunks
11+
echo "Spliiting file ${filename} into $numinput parts."
912
split -n $numinput $filename $filename-
13+
ls $filename-* | sort > files.txt
14+
cat files.txt
1015

11-
# this will generate an uplaod id
16+
# This will generate the key and an upload id
1217
aws s3api create-multipart-upload --bucket dips-multipart-test --key $filename > key_uploadId.json
1318
uploadId=$(sed -n '/UploadId/ s/.*\: //p' key_uploadId.json | sed 's/\(\"\|\"\)//g')
1419
echo " "
15-
echo "UploadID is ${uploadId}"
20+
echo "aws s3api UploadID: ${uploadId}"
1621

17-
# Upload each part
22+
# Perform file part count
1823
echo " "
1924
totalFiles=$(ls -l ${filename}-* | wc -l)
2025
i=1
2126
letter=a
2227

23-
while [ $i <= $totalFiles ]
28+
# Create list.json file.
29+
echo '{"Parts":[' > list.json
30+
31+
# Loop through the filelist and upload
32+
while [ $i -le $totalFiles ];
2433
do
25-
echo "Uploading ${filename} part ${i}"
26-
aws s3api upload-part --bucket dips-multipart-test --key $filename --part-number $i --body $filename-a$letter --upload-id $uploadId
27-
filecount+=1
34+
bodyName=$(sed -n ${i}p files.txt)
35+
echo "Uploading ${bodyName} part ${i}"
36+
ETag=$(aws s3api upload-part --bucket dips-multipart-test --key ${filename} --part-number ${i} --body ${bodyName} --upload-id ${uploadId} | sed -n '/ETag/ s/.*\: //p')
37+
38+
if [ $i -eq $totalFiles ]; then
39+
echo '{ "PartNumber": '${i}', "ETag": '${ETag}'}]}' >> list.json
40+
else
41+
echo '{ "PartNumber": '${i}', "ETag": '${ETag}'},' >> list.json
42+
fi
43+
44+
((i++))
2845
letter=$(echo "$letter" | tr "0-9a-z" "1-9a-z_")
46+
echo " "
2947
done
48+
49+
# Get the parts
50+
aws s3api list-parts --bucket dips-multipart-test --key $filename --upload-id $uploadId > parts.json
51+
52+
# After parts uploaded. Compile Etag and part number into one json file and upload.
53+
echo " "
54+
echo "Completing ${filename} multipart-upload"
55+
aws s3api complete-multipart-upload --multipart-upload file://list.json --bucket dips-multipart-test --key $filename --upload-id $uploadId
3056
}
3157

3258
############################# MAIN MENU FUNCTION ########################################
@@ -45,7 +71,7 @@ mainmenu () {
4571
echo ""
4672
echo "Press x to exit the script"
4773
echo ""
48-
read -n 2 -p "Input Selection: " filename
74+
read -p "Input Selection: " filename
4975
echo ""
5076

5177
if [ "$filename" = "x" ]; then
@@ -55,10 +81,4 @@ mainmenu () {
5581
fi
5682
}
5783

58-
mainmenu
59-
60-
# # Get all the ETags and part numbers
61-
# aws s3api list-parts --bucket dips-multipart-test --key <key_name_from_step_2> --upload-id $uploadId
62-
63-
# # After parts uploaded. Compile Etag and part number into one json file and upload.
64-
# aws s3api complete-multipart-upload --multipart-upload file://<jsonfilename> --bucket dips-multipart-test --key <key_name_from_step_2> --upload-id $uploadId
84+
mainmenu

0 commit comments

Comments
 (0)