-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
322 lines (257 loc) · 7.77 KB
/
index.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>AWS IO Example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<script type="text/javascript" src="/js/lib/dummy.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style id="compiled-css" type="text/css">
.box {
border: solid 1px;
padding: 5px;
margin: 5px;
font-size: 12px;
text-align: center;
background-color: white;
}
.box.container {
display: inline-block;
border: none;
}
.box.container.parent {
display: inline-block;
border: solid 1px;
border-radius: 10px;
}
.box.button {
font-weight: bold;
font-size: 16px;
border-radius: 5px;
}
.box.floating {
float: left;
margin: 15px;
padding: 10px;
font-weight: bold;
border-radius: 5px;
}
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
button.accordion.active,
button.accordion:hover {
background-color: #ddd;
}
button.accordion:after {
content: '\02795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
button.accordion.active:after {
content: "\2796";
}
div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: 0.6s ease-in-out;
opacity: 0;
}
div.panel.show {
opacity: 1;
max-height: 500px;
}
div.panellast {
padding: 0 18px;
background-color: white;
max-height: 0;
transition: 0.6s ease-in-out;
opacity: 0;
display: inline-block;
}
div.panellast.show {
opacity: 1;
}
</style>
<script type="text/javascript">
window.onload=function(){
//Configuration settings for S3
var s3BucketName = '...'; //Bucket name.
var s3Region = 'ap-northeast-2'; //Region where bucket is hosted.
var s3Key = '...'; //Security key used for external access.
var s3Secret = '...'; //Secret used for external access.
//------------------------------------
var currentDir = "";
//------------------------------------
//Set the key and secret in order to allow access, should be done before instantiating the AWS.S3 object.
AWS.config.update({
accessKeyId: s3Key,
secretAccessKey: s3Secret,
region: s3Region
});
function roundToTwo(num) {
return +(Math.round(num + "e+2") + "e-2");
}
function getFileSize(filek) {
if (filek > 1024) {
filek = Math.round(filek / 1024);
if (filek > 1024) {
filek = roundToTwo(filek / 1024);
return filek + "MB"
}
return filek + "KB";
}
return filek + "B";
}
function getParentFolder(fullpath) {
var lastChar = fullpath.substr(-1); // Selects the last character
if (lastChar !== '/') { // is not directory
var parts = fullpath.split("/");
var beforeLast = parts[parts.length - 1];
return fullpath.substr(0, fullpath.length - beforeLast.length);
} else {
return getParentFolder(fullpath.substr(0, fullpath.length - 1));
}
}
function encode(data) {
var str = data.reduce(function(a, b) {
return a + String.fromCharCode(b)
}, '');
return btoa(str).replace(/.{76}(?=.)/g, '$&\n');
}
//The function that our list button generates.
function listObjs(directory = '') {
var startTime = new Date().getTime();
currentDir = directory;
var s3 = new AWS.S3({
params: {
Bucket: s3BucketName, //Set the bucket name.
Delimiter: '/',
Prefix: directory,
signatureVersion: 'v4'
}
});
var objListing = s3.listObjects(function(error, data) {
if (error === null) {
var html_keys = '';
var html_imgs = '';
var html_dirs = '';
jQuery.each(data.Contents, function(index, obj) {
html_keys += '' + (index + ': ' + obj.Key + ' (' + getFileSize(obj.Size) + ')' + '<br />');
/**/
var lastChar = obj.Key.substr(-1); // Selects the last character
if (lastChar !== '/') { // is not directory
html_imgs += "<div id='image-preview-" + index + "' class='box floating'><img src='" + "https://s3-" + s3Region + ".amazonaws.com/" + s3BucketName + "/" + obj.Key + "' width='100px' height='100px'/><br />" + obj.Key + " (" + getFileSize(obj.Size) + ")</div>";
} else {
var prevdir = getParentFolder(directory);
//Will show the name of the directory we are inside.
html_dirs += "<div class='box floating'><img src='http://image.flaticon.com/icons/png/512/0/340.png' width='100px' height='100px' onClick='listObjs(" + '"' + prevdir + '"' + ")' /><br />Return to '" + prevdir + "'</div>";
}
});
jQuery.each(data.CommonPrefixes, function(index, obj) {
html_dirs += "<div class='box floating'><img src='http://findicons.com/files/icons/727/leopard/128/folder.png' width='100px' height='100px' onClick='listObjs(" + '"' + obj.Prefix + '"' + ")' /><br />" + obj.Prefix + "</div>";
});
jQuery("#objKeys").html(html_keys);
jQuery("#objImgs").html(html_dirs + html_imgs);
var timeToLoad = (new Date().getTime() - startTime) / 1000.0;
jQuery("#objTiming").html("Loaded listing in " + timeToLoad + " seconds.");
} else {
console.log(error);
}
});
}
function downloadFile(fileKey, index) {
var s3 = new AWS.S3({
params: {
Bucket: s3BucketName, //Set the bucket name.
}
});
s3.getObject({
Key: fileKey
}, function(err, file) {
var imgg = document.createElement('img');
imgg.src = URL.createObjectURL(new Blob(file.Body, {
type: 'image/png'
}));
jQuery("#image-preview-" + index).append(imgg);
});
}
function uploadFile() {
var s3 = new AWS.S3({
params: {
Bucket: s3BucketName,
Delimiter: '/',
Prefix: currentDir
}
});
var file = document.getElementById('fileToUpload').files[0];
if (file) {
s3.putObject({
Key: currentDir + file.name,
ContentType: file.type,
Body: file,
ACL: "public-read"
}, function(err, data) {
if (data !== null) {
//alert("Got it!");
} else {
alert("Upload failed!");
}
listObjs(currentDir);
});
}
}
//----------------------------------------------
//Accordion stuff
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
</script>
<script type="text/javascript" src="//code.jquery.com/jquery-3.1.1.js"></script>
<script type="text/javascript" src="https://blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script>
<script type="text/javascript" src="https://sdk.amazonaws.com/js/aws-sdk-2.7.6.min.js"></script>
</head>
<body>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<div>
<div class="box container parent">
<button class="box button" onclick="listObjs()">Load AWS S3 Bucket</button>
<div id="objTiming"></div>
<button class="accordion">S3 Object Key List</button>
<div class="panel">
<div id="objKeys"></div>
</div>
<button class="accordion">Upload</button>
<div class="panel">
<input type="file" class="box button" id="fileToUpload">
<button class="box button" onclick="uploadFile()">Upload</button>
<br> (Uploads to current directory)
</div>
<button class="accordion active">Files</button>
<div id="foo" class="panellast show">
<div class="box container" id="objImgs"></div>
</div>
</div>
</div>
</body></html>