-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
75 lines (65 loc) · 2.6 KB
/
upload.js
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
// Remember to include jQuery somewhere.
$(function() {
$('#theForm').on('submit', sendFile);
});
var imagenetApiEndpoint = "http://imagenet-nlb-b4fcd380459e9b61.elb.us-east-2.amazonaws.com";
var metadata = {};
function sendFile(e) {
//document.write(9);
// get the reference to the actual file in the input
e.preventDefault();
var theFormFile = $('#theFile').get()[0].files[0];
$.ajax({
url: imagenetApiEndpoint + '/metadata?query_class=' + $('input[name="uploadClass"]').val(),
type: 'GET',
success: function(data) {
// returnedImage = '';
// returnedImage = '<div>';
metadata = JSON.parse(JSON.stringify(data));
if (metadata !== {}) {
console.log(uploadPreSignedUrl);
console.log(metadata.index);
uploadPreSignedUrl = s3.getSignedUrl('putObject', {
Bucket: 'imagenetbucket',
Key: 'images/' + metadata.index.toString() + '.jpg',
ACL: 'authenticated-read',
// This must match with your ajax contentType parameter
ContentType: 'binary/octet-stream',
Metadata: metadata
/* then add all the rest of your parameters to AWS puttObect here */
});
console.log(uploadPreSignedUrl);
$.ajax({
type: 'PUT',
url: uploadPreSignedUrl,
// Content type must much with the parameter you signed your URL with
contentType: 'binary/octet-stream',
// this flag is important, if not set, it will try to send data as a form
processData: false,
// the actual file is sent raw
data: theFormFile,
Metadata: metadata
});
document.getElementById("query_input").value = metadata.class;
$("#textform").submit()
} else {
alert('Enter an imagenet class')
}
}
});
return false;
};
var s3 = new AWS.S3({
region: 'us-east-2',
signatureVersion: 'v4',
accessKeyId: 'REPLACE ME WITH YOUR ACCESS KEY ID',
secretAccessKey: 'REPLACE ME WITH YOUR SECRET ACCESS KEY'
});
var uploadPreSignedUrl = s3.getSignedUrl('putObject', {
Bucket: 'imagenetbucket',
Key: 'images/test.jpg',
ACL: 'authenticated-read',
// This must match with your ajax contentType parameter
ContentType: 'binary/octet-stream'
/* then add all the rest of your parameters to AWS puttObect here */
});