-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (137 loc) · 5.04 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Merge content from blobs into one blob */
/* The configuration is here */
/* Modify the configuration and npm start */
const inputContainerName = 'firstdata';
const inputFolder = 'exception/';
const outputContainerName = 'firstdata';
const blobName = 'output/exception.txt';
/* End configuration-----------------------*/
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}
const path = require('path');
const storage = require('azure-storage');
const blobService = storage.createBlobService();
const listContainers = async () => {
return new Promise((resolve, reject) => {
blobService.listContainersSegmented(null, (err, data) => {
if (err) {
reject(err);
} else {
resolve({message: `${data.entries.length} containers`, containers: data.entries});
}
});
});
};
const createContainer = async (containerName) => {
return new Promise((resolve, reject) => {
blobService.createContainerIfNotExists(containerName, {publicAccessLevel: 'blob'}, err => {
if (err) {
reject(err);
} else {
resolve({message: `Container '${containerName}' created`});
}
});
});
};
const uploadString = async (containerName, blobName, text) => {
return new Promise((resolve, reject) => {
blobService.createAppendBlobFromText(containerName, blobName, text, err => {
if (err) {
reject(err);
} else {
resolve({message: `Text "${text}" is written to blob storage`});
}
});
});
};
const appendString = async (containerName, blobName, text) => {
return new Promise((resolve, reject) => {
blobService.appendFromText(containerName, blobName, text, (err, result, response) => {
if (err) {
console.log("error");
reject(err);
} else {
console.log("write success block of three thousand or less");
resolve({message: `Text "${text}" is written to blob storage`});
}
});
});
};
const listBlobs = async (containerName) => {
return new Promise((resolve, reject) => {
blobService.listBlobsSegmentedWithPrefix(containerName, inputFolder, null, (err, data) => {
if (err) {
reject(err);
} else {
resolve({message: `${data.entries.length} blobs in '${containerName}'`, blobs: data.entries});
}
});
});
};
const downloadBlob = async (containerName, blobName) => {
return new Promise((resolve, reject) => {
blobService.getBlobToText(containerName, blobName, (err, data) => {
if (err) {
reject(err);
} else {
resolve({message: `Blob downloaded "${data}"`, text: data});
}
});
});
};
const createContainerIfDoesNotExist = async (containerName) => {
response = await listContainers();
const containerDoesNotExist = response.containers.findIndex((container) => container.name === containerName) === -1;
if (containerDoesNotExist) {
await createContainer(containerName);
console.log(`Container "${containerName}" is created`);
}
};
function customComparator(a, b) {
if (a.length != b.length) {
return a.length - b.length;
} else {
return a < b ? -1 : 1;
}
}
const execute = async () => {
let response;
var blobArray = [];
var content = '';
// get all the blob name in a container
response = await listBlobs(inputContainerName);
response.blobs.forEach((blob) => {
blobArray.push(blob.name);
});
// sort all the blob names by custom comparator
blobArray.sort(customComparator);
// upload content into one blob to destination container
await createContainerIfDoesNotExist(outputContainerName);
await uploadString(outputContainerName, blobName, '');
console.log("START MERGING ...");
// get all content from blobs
for (var index in blobArray) {
response = await downloadBlob(inputContainerName, blobArray[index]);
console.log("read from: " + blobArray[index]);
console.log("output container name: " + outputContainerName);
console.log("blob name: " + blobName);
var arr = response.text.split('\n');
var count = 0;
var content = '';
for (var i in arr) {
if (count <= 3000) {
content += arr[i] + '\n';
count++;
} else {
await appendString(outputContainerName, blobName, content);
count = 0;
content = '\n';
}
}
if (content != '') {
await appendString(outputContainerName, blobName, content);
}
}
}
execute().then(() => console.log("Done")).catch((e) => console.log(e));