-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdata.sh
211 lines (178 loc) · 6.03 KB
/
userdata.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
# Create the index.html file at /var/www/html using heredoc
cat <<'EOF' > /var/www/html/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Detection using Amazon Rekognition</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h2 {
color: #333;
}
#uploadForm {
display: flex;
flex-direction: column;
align-items: flex-start;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.file-input {
padding: 10px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-bottom: 10px;
}
button {
padding: 10px 20px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
#progressContainer {
width: 100%;
background-color: #f0f0f0;
border-radius: 5px;
overflow: hidden;
margin-top: 20px;
}
#progressBar {
width: 0%;
height: 20px;
background-color: #28a745;
transition: width 0.4s ease;
}
#statusMessage {
margin-top: 10px;
font-size: 14px;
color: #555;
}
#errorMessage {
color: red;
}
</style>
</head>
<body>
<center><h2>Object Detection using Amazon Rekognition</h2></center>
<form id="uploadForm">
<input type="file" id="imageFile" class="file-input" name="file" accept="image/*" required>
<button type="submit">Upload Image</button>
<p id="errorMessage"></p>
</form>
<!-- Progress Bar -->
<div id="progressContainer" style="display: none;">
<div id="progressBar"></div>
</div>
<!-- Status Message -->
<p id="statusMessage"></p>
<script>
document.getElementById('uploadForm').onsubmit = async function(event) {
event.preventDefault();
const submitButton = document.querySelector('button');
const fileInput = document.getElementById('imageFile');
const file = fileInput.files[0];
// File type validation (Only allow JPG, PNG, JPEG)
if (!['image/jpeg', 'image/png'].includes(file.type)) {
document.getElementById('errorMessage').innerText = "Unsupported file type. Only JPG and PNG allowed.";
return;
}
const uniqueFileName = `image_${Date.now()}_${Math.floor(Math.random() * 10000)}.jpeg`;
// Convert to JPEG if it's PNG or JPG
const convertedFile = await convertToJpeg(file);
submitButton.disabled = true;
const progressContainer = document.getElementById('progressContainer');
const progressBar = document.getElementById('progressBar');
const statusMessage = document.getElementById('statusMessage');
progressContainer.style.display = 'block';
statusMessage.textContent = "Uploading...";
try {
// Step 1: Request a presigned URL
const response = await fetch(`https://API-GATEWAY-URL/STAGE-NAME/RESOURCE?file_name=${uniqueFileName}`);
const data = await response.json();
if (data.presigned_url) {
const xhr = new XMLHttpRequest();
xhr.open('PUT', data.presigned_url, true);
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
const percentComplete = (event.loaded / event.total) * 100;
progressBar.style.width = percentComplete + "%";
}
};
xhr.onload = function () {
if (xhr.status === 200) {
progressBar.style.width = "100%";
statusMessage.textContent = "File uploaded successfully!";
} else {
statusMessage.textContent = "Failed to upload file.";
}
submitButton.disabled = false;
};
xhr.onerror = function () {
statusMessage.textContent = "An error occurred during the upload.";
submitButton.disabled = false;
};
xhr.send(convertedFile);
} else {
statusMessage.textContent = "Failed to get presigned URL.";
submitButton.disabled = false;
}
} catch (error) {
statusMessage.textContent = "An error occurred during the upload process.";
submitButton.disabled = false;
}
};
function convertToJpeg(file) {
return new Promise((resolve) => {
if (file.type === 'image/jpeg') {
resolve(file);
} else {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.src = e.target.result;
img.onload = function() {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
resolve(new File([blob], file.name, { type: 'image/jpeg' }));
}, 'image/jpeg');
};
};
reader.readAsDataURL(file);
}
});
}
</script>
</body>
</html>
EOF
# Set the appropriate permissions (optional)
chown apache:apache /var/www/html/index.html
chmod 644 /var/www/html/index.html