-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
63 lines (57 loc) · 2 KB
/
storage.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
const storage = require('azure-storage');
const stream = require('stream');
const connectionString = "DefaultEndpointsProtocol=https;AccountName=airg19storage;AccountKey=PQFlqEXDRqPcAgcG8c+OJ/ymM33Ki9b8oS4Y0eQAH830v4Ixqs9NOOn0lCmGwytcBp0rn9lgBkko2H9QI6l4Tw==;EndpointSuffix=core.windows.net";
const containerName = "result-images";
const blobService = storage.createBlobService(connectionString);
const listContainers = async () => {
return new Promise((resolve, reject) => {
blobService.listContainersSegmented(null, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data.entries);
}
});
});
};
exports.updateResultImage = async(filename, imageBuffer) => {
return new Promise((resolve, reject) => {
blobService.createBlockBlobFromText(containerName, filename, imageBuffer, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
exports.downloadResultImage = async(filename) => {
return new Promise((resolve, reject) => {
let bufferArray = [];
const ws = new stream.Writable({
write(chunk, encoding, callback) {
bufferArray.push(...chunk)
callback();
}
});
ws.on('finish', () => {
let dataBuffer = Buffer.from(bufferArray);
resolve(dataBuffer);
});
ws.on('error', (err) => {
reject(err);
});
blobService.getBlobToStream(containerName, filename, ws, (err, data) => {
if (err) {
reject(err);
}
});
});
};
exports.connect = async() => {
const containers = await listContainers();
const isContainerNotExist = containers.length < 1 || containers.findIndex((container) => container.name === containerName) === -1;
if (isContainerNotExist) {
throw Error(`Container "${containerName}" does not exist!`);
}
};