-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3-upload-test.html
73 lines (64 loc) · 2.22 KB
/
s3-upload-test.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Kuzzle Plugin S3</title>
<script src="https://cdn.jsdelivr.net/npm/kuzzle-sdk/dist/kuzzle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<h1>
Kuzzle Plugin S3
</h1>
<form>
<img id="uploadedImage" src="" width="125" height="125"/>
<input id="uploadInput" type="file" onChange="uploadTempFile()"/>
<input id="fileKey" type="hidden"/>
<button onclick="submitFile()">Submit</button>
</form>
<button onclick="listFiles()">ListFiles</button>
<pre id="filesKeys">List of files keys</pre>
<script type="application/javascript">
(async function () {
window.kuzzle = new KuzzleSDK.Kuzzle(new KuzzleSDK.WebSocket('localhost'))
await window.kuzzle.connect()
})();
async function uploadTempFile() {
const file = document.getElementById('uploadInput').files[0];
// Get a Presigned URL
const { result } = await window.kuzzle.query({
controller: 's3/upload',
action: 'getUrl',
uploadDir: 'xen',
filename: file.name
});
// Upload the file directly to S3
const axiosOptions = {
headers: {
'Content-Type': file.type
}
};
await axios.put(result.uploadUrl, file, axiosOptions);
document.getElementById('fileKey').value = result.fileKey;
document.getElementById('uploadedImage').src = result.fileUrl;
}
async function submitFile() {
const fileKey = document.getElementById('fileKey').value;
// Validate the uploaded file
const { result } = await window.kuzzle.query({
controller: 's3/upload',
action: 'validate',
fileKey
});
}
async function listFiles() {
// Get a Presigned URL
const {result } = await window.kuzzle.query({
controller: 's3/file',
action: 'getFilesKeys'
});
document.getElementById('filesKeys').innerHTML = JSON.stringify(result, undefined, 2);;
}
</script>
</body>
</html>