1
1
#! /bin/bash
2
+ # Created by Hasshim Ali
3
+ # Version 1.0
2
4
# This script is used to split files and upload them to S3 using the multipart upload.
3
5
# 1. Execute the script, set the number of files to be splitted.
4
6
# 2. Provide the filename.
7
+ # ###########################################################################
5
8
6
9
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 ."
9
12
split -n $numinput $filename $filename -
13
+ ls $filename -* | sort > files.txt
14
+ cat files.txt
10
15
11
- # this will generate an uplaod id
16
+ # This will generate the key and an upload id
12
17
aws s3api create-multipart-upload --bucket dips-multipart-test --key $filename > key_uploadId.json
13
18
uploadId=$( sed -n ' /UploadId/ s/.*\: //p' key_uploadId.json | sed ' s/\(\"\|\"\)//g' )
14
19
echo " "
15
- echo " UploadID is ${uploadId} "
20
+ echo " aws s3api UploadID: ${uploadId} "
16
21
17
- # Upload each part
22
+ # Perform file part count
18
23
echo " "
19
24
totalFiles=$( ls -l ${filename} -* | wc -l)
20
25
i=1
21
26
letter=a
22
27
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 ];
24
33
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++ ))
28
45
letter=$( echo " $letter " | tr " 0-9a-z" " 1-9a-z_" )
46
+ echo " "
29
47
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
30
56
}
31
57
32
58
# ############################ MAIN MENU FUNCTION ########################################
@@ -45,7 +71,7 @@ mainmenu () {
45
71
echo " "
46
72
echo " Press x to exit the script"
47
73
echo " "
48
- read -n 2 - p " Input Selection: " filename
74
+ read -p " Input Selection: " filename
49
75
echo " "
50
76
51
77
if [ " $filename " = " x" ]; then
@@ -55,10 +81,4 @@ mainmenu () {
55
81
fi
56
82
}
57
83
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