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([ '', '' + fileFilename + '', '' + fileSize + ' kB', '' + lastModified + '', '', '' ]); }); var message = files.length ? '

File List

' : '

You do not have any files for this device. Please add files.

'; var htmlTemplate = [ '

', 'DeviceId: ' + deviceId, '

', message, '
', '', '', '', getHtml(files), '', '
FilenameFile SizeLast Modified
', '
', '', '', ] 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); }); }