/
aws-s3-bucket-upload.html
159 lines (124 loc) · 5.23 KB
/
aws-s3-bucket-upload.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
<!doctype html>
<head>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.357.0.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="js/amazon-cognito-identity.min.js"></script>
<script src="js/config.js"></script>
<style>
.webbar{
background-color: #070097;
height: 60px;
border-bottom: 10px solid grey;
}
</style>
<script type="text/javascript">
//Bucket Configurations
var bucketName = "zeeshantest";
var bucketRegion = "ap-south-1";
var IdentityPoolId = "ap-south-1:e4ade473-da54-4967-9e7c-8cb77e645fd8";
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: bucketName}
});
</script>
</head>
<body>
<div class=" container-fluid webbar">
<div class="row">
<div class="col-md-6" style="color: orange; font-size:25px; text-align:start;">
<div style="padding-left: 30px;font-weight: bolder;"> Welcome!</div>
</div>
<div class="col-md-6" style="color: orange; font-size:25px; text-align:end;">
<label id="email_value" style="padding-right:20px;"></label>
<button type="button" onclick="signOut()" class="btn btn-danger">Logout</button>
</div>
</div>
</div>
<!-- A functional html code-->
<!-- <div>
<input type="file" id="fileUpload">
</div>
<div>
<button onclick="s3upload()">Submit</button>
</div>
<progress max=”100” value=”0”></progress> -->
<div class="container-fluid justify-content-center d-flex" style="height: 300px; background-color: greenyellow; border-radius: 0px 0px 900px 900px;">
<div class="row justify-content-center align-content-center">
<div class="col-md-12 align-content-center">
<input type="file" id="fileUpload">
<br>
<button type="button" class="btn btn-primary" onclick="signOut()" style="margin-top: 50px;">Upload File</button>
</div>
</div>
</div>
<script type="text/javascript">
var data = {
UserPoolId : _config.cognito.userPoolId,
ClientId : _config.cognito.clientId
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
var cognitoUser = userPool.getCurrentUser();
window.onload = function(){
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
alert(err);
return;
}
console.log('session validity: ' + session.isValid());
//Set the profile info
cognitoUser.getUserAttributes(function(err, result) {
if (err) {
console.log(err);
return;
}
console.log(result);
console.log(result[3].getValue());
console.log(result[2].getValue());
console.log(result[1].getValue());
document.getElementById("email_value").innerHTML = result[3].getValue();
});
});
}
}
function s3upload() {
console.log(bucketRegion)
console.log(bucketName)
var files = document.getElementById('fileUpload').files;
if (files)
{
var file = files[0];
var fileName = file.name;
var filePath = 'my-first-bucket-path/' + fileName;
var fileUrl = 'https://' + bucketRegion + '.amazonaws.com/my-first-bucket/' + filePath;
s3.upload({
Key: filePath,
Body: file,
ACL: 'public-read'
}, function(err, data) {
if(err) {
reject('error');
}
alert('Successfully Uploaded!');
}).on('httpUploadProgress', function (progress) {
var uploaded = parseInt((progress.loaded * 100) / progress.total);
$("progress").attr('value', uploaded);
});
}
};
function signOut(){
if (cognitoUser != null) {
cognitoUser.signOut();
window.location.href = "register.html";
}
}
</script>
</body>
</html>