Skip to content

Commit 08388ac

Browse files
committed
Add Amazon S3 query node.
1 parent fd5011e commit 08388ac

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

aws/s3.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,49 @@
1414
limitations under the License.
1515
-->
1616

17+
<script type="text/x-red" data-template-name="amazon s3">
18+
<div class="form-row">
19+
<label for="node-input-aws"><i class="fa fa-user"></i> AWS</label>
20+
<input type="text" id="node-input-aws">
21+
</div>
22+
<div class="form-row">
23+
<label for="node-input-bucket"><i class="fa fa-folder"></i> Bucket</label>
24+
<input type="text" id="node-input-bucket" placeholder="bucket name">
25+
</div>
26+
<div class="form-row node-input-filename">
27+
<label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
28+
<input type="text" id="node-input-filename" placeholder="Filename">
29+
</div>
30+
<div class="form-row">
31+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
32+
<input type="text" id="node-input-name" placeholder="Name">
33+
</div>
34+
</script>
35+
36+
<script type="text/x-red" data-help-name="amazon s3">
37+
<p>Amazon S3 input node. Downloads content from an Amazon S3 bucket. The bucket name can be specified in the node <b>bucket</b> property or in the <b>msg.bucket</b> property. The name of the file to download is taken from the node <b>filename</b> property or the <b>msg.filename</b> property. The downloaded content is sent as <b>msg.payload</b> property. If the download fails <b>msg.error</b> will contain an error object.</p>
38+
</script>
39+
40+
<script type="text/javascript">
41+
RED.nodes.registerType('amazon s3',{
42+
category: 'storage-output',
43+
color:"#C0DEED",
44+
defaults: {
45+
aws: {type:"aws-config",required:true},
46+
bucket: {required:true},
47+
filename: {value:""},
48+
name: {value:""},
49+
},
50+
inputs:1,
51+
outputs:1,
52+
icon: "amazon.png",
53+
align: "right",
54+
label: function() {
55+
return this.bucket ? "s3 "+this.bucket : "s3";
56+
}
57+
});
58+
</script>
59+
1760
<script type="text/x-red" data-template-name="amazon s3 out">
1861
<div class="form-row">
1962
<label for="node-input-aws"><i class="fa fa-user"></i> AWS</label>
@@ -50,6 +93,7 @@
5093
bucket: {required:true},
5194
filename: {value:""},
5295
localFilename: {value:""},
96+
name: {value:""},
5397
},
5498
inputs:1,
5599
outputs:0,

aws/s3.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,52 @@ module.exports = function(RED) {
1818
"use strict";
1919
var fs = require('fs');
2020

21+
function AmazonS3QueryNode(n) {
22+
RED.nodes.createNode(this,n);
23+
this.awsConfig = RED.nodes.getNode(n.aws);
24+
this.region = n.region;
25+
this.bucket = n.bucket;
26+
this.filename = n.filename || "";
27+
var node = this;
28+
var AWS = this.awsConfig ? this.awsConfig.AWS : null;
29+
if (!AWS) {
30+
node.warn("Missing AWS credentials");
31+
return;
32+
}
33+
var s3 = new AWS.S3();
34+
node.on("input", function(msg) {
35+
msg.filename = filename;
36+
var bucket = node.bucket || msg.bucket;
37+
if (bucket === "") {
38+
node.warn("No bucket specified");
39+
return;
40+
}
41+
var filename = node.filename || msg.filename;
42+
if (filename === "") {
43+
node.warn("No filename specified");
44+
return;
45+
}
46+
msg.filename = filename;
47+
node.status({fill:"blue",shape:"dot",text:"downloading"});
48+
s3.getObject({
49+
Bucket: bucket,
50+
Key: filename,
51+
}, function(err, data) {
52+
if (err) {
53+
node.warn("download failed " + err.toString());
54+
delete msg.payload;
55+
msg.error = err;
56+
} else {
57+
msg.payload = data.Body;
58+
delete msg.error;
59+
}
60+
node.status({});
61+
node.send(msg);
62+
});
63+
});
64+
}
65+
RED.nodes.registerType("amazon s3", AmazonS3QueryNode);
66+
2167
function AmazonS3OutNode(n) {
2268
RED.nodes.createNode(this,n);
2369
this.awsConfig = RED.nodes.getNode(n.aws);
@@ -34,25 +80,25 @@ module.exports = function(RED) {
3480
if (AWS) {
3581
var s3 = new AWS.S3();
3682
node.status({fill:"blue",shape:"dot",text:"checking credentials"});
37-
s3.listObjects({ Bucket: this.bucket }, function(err) {
83+
s3.listObjects({ Bucket: node.bucket }, function(err) {
3884
if (err) {
3985
node.error("AWS S3 error: " + err);
4086
node.status({fill:"red",shape:"ring",text:"error"});
4187
return;
4288
}
4389
node.status({});
4490
node.on("input", function(msg) {
45-
var bucket = this.bucket || msg.bucket;
91+
var bucket = node.bucket || msg.bucket;
4692
if (bucket === "") {
4793
node.warn("No bucket specified");
4894
return;
4995
}
50-
var filename = this.filename || msg.filename;
96+
var filename = node.filename || msg.filename;
5197
if (filename === "") {
5298
node.warn("No filename specified");
5399
return;
54100
}
55-
var localFilename = this.localFilename || msg.localFilename;
101+
var localFilename = node.localFilename || msg.localFilename;
56102
if (localFilename) {
57103
// TODO: use chunked upload for large files
58104
node.status({fill:"blue",shape:"dot",text:"uploading"});

0 commit comments

Comments
 (0)