-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3-bucket-upload.htm
201 lines (167 loc) · 6.15 KB
/
s3-bucket-upload.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Upload web page</title>
</head>
<body>
<script src="./script/jquery.min.js"></script>
<script src="./script/aws-sdk-2.487.0.min.js"></script>
<script src="./script/aws-cognito-sdk.min.js"></script>
<script src="./script/amazon-cognito-identity.min.js"></script>
</br>
<input id="fileUpload" type="file" /></br>
<br>
<input id="uploadS3ObjectsButton" type="button" value="upload" onclick="s3upload()">
</br></br>
<input id="clearLogsButton" type="button" value="Clear Logs" onclick="clearLogs()">
</br></br>
<div id="loader" class="loader" style="display:none"></div>
</br></br>
<div id="log"></div>
<script>
//=============== AWS IDs ===============
var userPoolId = 'us-east-2_tTtestHHI';
var clientId = '1mhis8testingr9chd8tvdc2n4gs';
var region = 'us-east-2';
var identityPoolId = 'us-east-2:5d646adc-test-4193-aa09-86c5rt3c8';
var bucketName = 'testbucket';
var bucketRegion = 'us-east-2';
var IdentityPoolId = 'us-east-2:test-b1ef-4193-aa09-86c5e5551cc8';
//=============== AWS IDs ===============
var cognitoUser;
var idToken;
var userPool;
var poolData = {
UserPoolId : userPoolId,
ClientId : clientId
};
getCurrentLoggedInSession();
function clearLogs(){
$('#log').empty();
}
/*
Starting point for user logout flow
*/
function logOut(){
if (cognitoUser != null) {
$("#loader").show();
cognitoUser.signOut();
switchToLogInView();
logMessage('Logged out!');
$("#loader").hide();
}
}
/*
Starting point for user login flow with input validation
*/
/*
Starting point for AWS List S3 Objects flow with input validation
*/
function s3upload() {
var files = document.getElementById('fileUpload').files;
if (files)
{
var file = files[0];
var fileName = file.name;
var bucketRegion = "us-east-2";
var filePath = 'my-first-bucket-path/' + fileName;
var fileUrl = 'https://' + bucketRegion + '.amazonaws.com/my-first-bucket/' + filePath;
var s3 = new AWS.S3();
var upload = new AWS.S3.ManagedUpload({
params: {
Bucket: bucketName,
Key: fileName,
Body: file
}
});
var promise = upload.promise();
promise.then(
function(data) {
alert("Successfully uploaded file.");
},
function(err) {
return alert("There was an error uploading your photo: ", err.message);
}
)
}
}
/*
This method will get temporary credentials for AWS using the IdentityPoolId and the Id Token recieved from AWS Cognito authentication provider.
*/
function getCognitoIdentityCredentials(){
AWS.config.region = region;
var loginMap = {};
loginMap['cognito-idp.' + region + '.amazonaws.com/' + userPoolId] = idToken;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins: loginMap
});
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: bucketName}
})
AWS.config.credentials.clearCachedId();
AWS.config.credentials.get(function(err) {
if (err){
logMessage(err.message);
}
else {
logMessage('As authenticated user you can upload the files to the buckets');
}
$("#loader").hide();
});
}
/*
This method will use AWS S3 SDK to get a list of S3 bucket object.
Before using this method, AWS Credentials must be set in AWS config.
*/
function getAWSS3BucketObjects(){
var s3 = new AWS.S3();
var params = {
Bucket: $("#bucketNameInput").val()
};
s3.listObjects(params, function(err, data) {
if (err) logMessage(err.message);
else{
logMessage('');
logMessage('====== S3 Bucket Objects ======');
data.Contents.forEach(element => {
logMessage(element.Key);
});
logMessage('');
}
$("#loader").hide();
});
}
/*
If user has logged in before, get the previous session so user doesn't need to log in again.
*/
function getCurrentLoggedInSession(){
$("#loader").show();
userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
cognitoUser = userPool.getCurrentUser();
if(cognitoUser != null){
cognitoUser.getSession(function(err, session) {
if (err) {
logMessage(err.message);
}else{
logMessage('Session found! Logged in.');
idToken = session.getIdToken().getJwtToken();
getCognitoIdentityCredentials();
}
$("#loader").hide();
});
}else{
logMessage('Session expired. Please log in again.');
$("#loader").hide();
}
}
/*
This is a logging method that will be used throught the application
*/
function logMessage(message){
$('#log').append(message + '</br>');
}
</script>
</body>
</html>