-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazureblobstorage-get.js
96 lines (79 loc) · 3.64 KB
/
azureblobstorage-get.js
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
/*jshint esversion: 6 */
module.exports = function (RED) {
const fs = require('fs');
const os = require('os');
const path = require('path');
const azure = require('@azure/storage-blob');
const statusEnum = {
disconnected: { color: "grey", text: "Disconnected" },
sending: { color: "yellow", text: "Sending" },
blobSaved: { color: "green", text: "Blob Saved" },
blobDownloaded: { color: "green", text: "Blob downloaded" },
error: { color: "red", text: "Error" },
receiving: { color: "yellow", text: "Receiving" },
received: { color: "green", text: "Received message" },
operational: { color: "blue", text: "Operational" }
};
const setStatus = (node, status) => {
node.status({ fill: status.color, shape: "dot", text: status.text });
};
function azureStorageGetBlob(config) {
const node = this;
RED.nodes.createNode(node, config);
const tempDir = path.join(os.tmpdir(), 'aleph-get-blob-');
const tempDirectory = fs.mkdtempSync(tempDir);
node.log(`Temp directory: ${tempDirectory}`);
const clientAccountName = node.credentials?.accountname;
const clientAccountKey = node.credentials?.key;
const azureStorageBlobContainerName = node.credentials?.container;
const azureStorageBlobConnectionString = `DefaultEndpointsProtocol=https;AccountName=${clientAccountName};AccountKey=${clientAccountKey};EndpointSuffix=core.windows.net`;
setStatus(node, statusEnum.operational);
this.on('input', async (msg, send, done) => {
setStatus(node, statusEnum.receiving);
let destinationFile;
let clientBlobName;
try {
if (msg.payload) {
if (!msg.payload.destinationFile) {
done('No destinationFile parameter');
return;
}
destinationFile = path.join(tempDirectory, msg.payload.destinationFile);
clientBlobName = msg.payload.blobName || node.credentials.blob;
if (!clientBlobName) {
done('No BlobName defined');
return;
}
}
else {
clientBlobName = node.credentials.blob;
const fileName = clientBlobName.replace('.txt', '.downloaded.txt');
destinationFile = path.join(tempDirectory, fileName);
}
const containerClient = new azure.ContainerClient(azureStorageBlobConnectionString, azureStorageBlobContainerName);
const blockBlobClient = containerClient.getBlockBlobClient(clientBlobName);
await blockBlobClient.downloadToFile(destinationFile);
msg.payload.blobName = clientBlobName;
msg.status = "OK";
send(msg);
setStatus(node, statusEnum.blobDownloaded);
done();
} catch (error) {
setStatus(node, statusEnum.error);
done(`Error while trying to save blob: ${error.toString()}`);
this.error(`Error while trying to save blob: ${error.toString()}`, msg);
}
});
}
RED.nodes.registerType("Aleph Get Blob", azureStorageGetBlob, {
credentials: {
accountname: { type: "text" },
key: { type: "text" },
container: { type: "text" },
blob: { type: "text" },
},
defaults: {
name: { value: "Get Blob Storage" },
}
});
};