Skip to content

Commit

Permalink
Add S3 watch node.
Browse files Browse the repository at this point in the history
  • Loading branch information
hindessm committed Oct 21, 2014
1 parent 08388ac commit e819829
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
47 changes: 47 additions & 0 deletions aws/s3.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,53 @@
limitations under the License.
-->

<script type="text/x-red" data-template-name="amazon s3 in">
<div class="form-row">
<label for="node-input-aws"><i class="fa fa-user"></i> AWS</label>
<input type="text" id="node-input-aws">
</div>
<div class="form-row">
<label for="node-input-bucket"><i class="fa fa-folder"></i> Bucket</label>
<input type="text" id="node-input-bucket" placeholder="bucket name">
</div>
<div class="form-row node-input-filepattern">
<label for="node-input-filepattern"><i class="fa fa-file"></i> Filename Pattern</label>
<input type="text" id="node-input-filepattern" placeholder="Filepattern">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>

<script type="text/x-red" data-help-name="amazon s3 in">
<p>Amazon S3 watch node. Watches for file events on Box. By default all
file events are reported, but the filename pattern can be supplied
to limit the events to files which have full filenames that match
the glob pattern. The event messages consist of the full filename
in <b>msg.payload</b> property, the filename in <b>msg.file</b>,
the event type in <b>msg.event</b>.</p>
</script>

<script type="text/javascript">
RED.nodes.registerType('amazon s3 in',{
category: 'storage-input',
color:"#C0DEED",
defaults: {
aws: {type:"aws-config",required:true},
bucket: {required:true},
filepattern: {value:""},
name: {value:""},
},
inputs:0,
outputs:1,
icon: "amazon.png",
label: function() {
return this.bucket ? "s3 "+this.bucket : "s3";
}
});
</script>

<script type="text/x-red" data-template-name="amazon s3">
<div class="form-row">
<label for="node-input-aws"><i class="fa fa-user"></i> AWS</label>
Expand Down
82 changes: 81 additions & 1 deletion aws/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,86 @@
module.exports = function(RED) {
"use strict";
var fs = require('fs');
var minimatch = require("minimatch");

function AmazonS3InNode(n) {
RED.nodes.createNode(this,n);
this.awsConfig = RED.nodes.getNode(n.aws);
this.region = n.region;
this.bucket = n.bucket;
this.filepattern = n.filepattern || "";
var node = this;
var AWS = this.awsConfig ? this.awsConfig.AWS : null;
if (!AWS) {
node.warn("Missing AWS credentials");
return;
}
var s3 = new AWS.S3();
node.status({fill:"blue",shape:"dot",text:"initializing"});
s3.listObjects({ Bucket: node.bucket }, function(err, data) {
if (err) {
node.error("failed to fetch S3 state: " + err);
node.status({fill:"red",shape:"ring",text:"error"});
return;
}
node.state = data.Contents.filter(function (e) {
return !node.filepattern || minimatch(e, node.filepattern);
}).map(function (e) {
return e.Key;
});
node.status({});
node.on("input", function(msg) {
node.status({fill:"blue",shape:"dot",text:"checking for changes"});
s3.listObjects({ Bucket: node.bucket }, function(err, data) {
if (err) {
node.warn("failed to fetch S3 state: " + err);
node.status({});
return;
}
node.status({});
var newState = data.Contents.filter(function (e) {
return !node.filepattern ||
minimatch(e, node.filepattern);
}).map(function (e) {
return e.Key;
});
var seen = {};
var i;
for (i = 0; i < node.state.length; i++) {
seen[node.state[i]] = true;
}
for (i = 0; i < newState.length; i++) {
if (seen[newState[i]]) {
delete seen[newState[i]];
} else {
msg.payload = newState[i];
msg.file = newState[i].substring(newState[i].lastIndexOf('/') + 1);
msg.event = 'add';
node.send(msg);
}
}
for (var k in seen) {
if (seen.hasOwnProperty(k)) {
msg.payload = k;
msg.file = k.substring(k.lastIndexOf('/') + 1);
msg.event = 'delete';
node.send(msg);
}
}
node.state = newState;
});
});
var interval = setInterval(function() {
node.emit("input", {});
}, 900000); // 15 minutes
node.on("close", function() {
if (interval !== null) {
clearInterval(interval);
}
});
});
}
RED.nodes.registerType("amazon s3 in", AmazonS3InNode);

function AmazonS3QueryNode(n) {
RED.nodes.createNode(this,n);
Expand All @@ -32,7 +112,6 @@ module.exports = function(RED) {
}
var s3 = new AWS.S3();
node.on("input", function(msg) {
msg.filename = filename;
var bucket = node.bucket || msg.bucket;
if (bucket === "") {
node.warn("No bucket specified");
Expand All @@ -43,6 +122,7 @@ module.exports = function(RED) {
node.warn("No filename specified");
return;
}
msg.bucket = bucket;
msg.filename = filename;
node.status({fill:"blue",shape:"dot",text:"downloading"});
s3.getObject({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies" : {
"aws-sdk": "^2.0.15",
"dropbox":"^0.10.3",
"minimatch": "^1.0.0",
"oauth":"~0.9.11",
"request":"~2.40.0",
"instagram-node":"0.5.1",
Expand Down

0 comments on commit e819829

Please sign in to comment.