Adds non-working script to upload/download document from S3
Works in local test, does not work on POCloud due to the scripts not being in the header file
This commit is contained in:
12
HTML/Files.html
Normal file
12
HTML/Files.html
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.92.0.min.js"></script>
|
||||||
|
<script src="https://s3.amazonaws.com/pocloud-scripts/s3files.js"></script>
|
||||||
|
<div id="fileApp"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function check(){
|
||||||
|
console.log('CHECKING DEVICE <%=channels["flowmonitor.low_flow_gpm"].techName %>')
|
||||||
|
checkDevice('<%=channels["flowmonitor.low_flow_gpm"].techName %>');
|
||||||
|
};
|
||||||
|
setTimeout(check, 10000);
|
||||||
|
|
||||||
|
</script>
|
||||||
16
HTML/Files_test.html
Normal file
16
HTML/Files_test.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.92.0.min.js"></script>
|
||||||
|
<!-- <script src="https://s3.amazonaws.com/pocloud-scripts/s3files.js"></script> -->
|
||||||
|
<script src="./s3files.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="fileApp"></div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// checkDevice('<%=channels["flowmonitor.low_flow_gpm"].techName %>');
|
||||||
|
checkDevice("flowmonitor_[c4:93:00:03:69:bc:01:40]");
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
142
HTML/s3files.js
Normal file
142
HTML/s3files.js
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
var dataBucketName = 'pocloud-data';
|
||||||
|
var bucketRegion = 'us-east-1';
|
||||||
|
var IdentityPoolId = 'us-east-1:4ba4f2d0-ff79-446e-a510-ac16e411d3cc';
|
||||||
|
|
||||||
|
AWS.config.update({
|
||||||
|
region: bucketRegion,
|
||||||
|
credentials: new AWS.CognitoIdentityCredentials({
|
||||||
|
IdentityPoolId: IdentityPoolId
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
function getHtml(template) {
|
||||||
|
return template.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
var s3 = new AWS.S3({
|
||||||
|
// apiVersion: '2006-03-01',
|
||||||
|
params: {Bucket: dataBucketName}
|
||||||
|
});
|
||||||
|
|
||||||
|
function fixDeviceId(deviceId){
|
||||||
|
deviceId = deviceId.replace(/:/g, "");
|
||||||
|
deviceId = deviceId.replace("[", "");
|
||||||
|
deviceId = deviceId.replace("]", "");
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkDevice(deviceId) {
|
||||||
|
deviceId = fixDeviceId(deviceId);
|
||||||
|
// deviceId = deviceId.trim();
|
||||||
|
if (!deviceId) {
|
||||||
|
return alert('Device names must contain at least one non-space character.');
|
||||||
|
}
|
||||||
|
if (deviceId.indexOf('/') !== -1) {
|
||||||
|
return alert('Device names cannot contain slashes.');
|
||||||
|
}
|
||||||
|
var deviceKey = encodeURIComponent(deviceId) + '/';
|
||||||
|
s3.headObject({Key: deviceKey}, function(err, data) {
|
||||||
|
if (!err) {
|
||||||
|
// return alert('Device already exists.');
|
||||||
|
return viewDevicefiles(deviceId);
|
||||||
|
}
|
||||||
|
if (err.code !== 'NotFound') {
|
||||||
|
return alert('There was an error creating your device: ' + err.message);
|
||||||
|
}
|
||||||
|
s3.putObject({Key: deviceKey}, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return alert('There was an error creating your device: ' + err.message);
|
||||||
|
}
|
||||||
|
// alert('Successfully created device.');
|
||||||
|
return viewDevicefiles(deviceId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function viewDevicefiles(deviceId) {
|
||||||
|
deviceId = fixDeviceId(deviceId);
|
||||||
|
var deviceFileKey = encodeURIComponent(deviceId) + '//';
|
||||||
|
s3.listObjects({Prefix: deviceFileKey}, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return alert('There was an error viewing your device: ' + err.message);
|
||||||
|
}
|
||||||
|
// `this` references the AWS.Response instance that represents the response
|
||||||
|
var href = this.request.httpRequest.endpoint.href;
|
||||||
|
var bucketUrl = href + dataBucketName + '/';
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
var files = data.Contents.map(function(file) {
|
||||||
|
var fileKey = file.Key;
|
||||||
|
var fileUrl = bucketUrl + encodeURIComponent(fileKey);
|
||||||
|
var fileFilename = fileKey.replace(deviceFileKey, '');
|
||||||
|
var fileSize = file.Size / 1000; // kB
|
||||||
|
var lastModified = file.LastModified;
|
||||||
|
console.log(file);
|
||||||
|
return getHtml([
|
||||||
|
'<tr>',
|
||||||
|
'<td><a href="https://s3.amazonaws.com/' + dataBucketName + '/' + fileKey + '">' + fileFilename + '</a></td>',
|
||||||
|
'<td>' + fileSize + ' kB</td>',
|
||||||
|
'<td>' + lastModified + '</td>',
|
||||||
|
'<td><button onclick="deleteFile(\'' + deviceId + "','" + fileKey + '\')">Delete</button>',
|
||||||
|
'</tr>'
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
var message = files.length ?
|
||||||
|
'<h2>File List</h2>' :
|
||||||
|
'<p>You do not have any files for this device. Please add files.</p>';
|
||||||
|
var htmlTemplate = [
|
||||||
|
'<h2>',
|
||||||
|
'DeviceId: ' + deviceId,
|
||||||
|
'</h2>',
|
||||||
|
message,
|
||||||
|
'<div class="row">',
|
||||||
|
'<table class="table">',
|
||||||
|
'<thead><tr><th>Filename</th><th>File Size</th><th>Last Modified</th><th></th></thead>',
|
||||||
|
'<tbody>',
|
||||||
|
getHtml(files),
|
||||||
|
'</tbody>',
|
||||||
|
'</table>',
|
||||||
|
'</div>',
|
||||||
|
'<input id="fileupload" type="file" accept="*">',
|
||||||
|
'<button id="addfile" onclick="addFile(\'' + deviceId +'\')">',
|
||||||
|
'Add File',
|
||||||
|
'</button>',
|
||||||
|
]
|
||||||
|
document.getElementById('fileApp').innerHTML = getHtml(htmlTemplate);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addFile(deviceId) {
|
||||||
|
deviceId = fixDeviceId(deviceId);
|
||||||
|
var files = document.getElementById('fileupload').files;
|
||||||
|
if (!files.length) {
|
||||||
|
return alert('Please choose a file to upload first.');
|
||||||
|
}
|
||||||
|
var file = files[0];
|
||||||
|
var fileName = file.name;
|
||||||
|
var deviceFileKey = encodeURIComponent(deviceId) + '//';
|
||||||
|
|
||||||
|
var fileKey = deviceFileKey + fileName;
|
||||||
|
s3.upload({
|
||||||
|
Key: fileKey,
|
||||||
|
Body: file,
|
||||||
|
ACL: 'public-read'
|
||||||
|
}, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return alert('There was an error uploading your file: ', err.message);
|
||||||
|
}
|
||||||
|
alert('Successfully uploaded file.');
|
||||||
|
viewDevicefiles(deviceId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteFile(deviceId, fileKey) {
|
||||||
|
deviceId = fixDeviceId(deviceId);
|
||||||
|
s3.deleteObject({Key: fileKey}, function(err, data) {
|
||||||
|
if (err) {
|
||||||
|
return alert('There was an error deleting your file: ', err.message);
|
||||||
|
}
|
||||||
|
alert('Successfully deleted file.');
|
||||||
|
viewDevicefiles(deviceId);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user