-
Notifications
You must be signed in to change notification settings - Fork 0
/
processContainer.mjs
83 lines (72 loc) · 3.46 KB
/
processContainer.mjs
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
import fs from 'fs';
import Azure from '@azure/storage-blob';
import debug from 'debug';
const debugController = debug('Controller');
const debugFile = debug('RemoteFile');
const debugDirectory = debug('Directory');
const fsPromises = fs.promises;
export default async function processContainer(container, storeDirectory, storageAccountName, serviceURL) {
const storeDirectoryWithContainer = `${storeDirectory}${storageAccountName}/${container.name}/`;
let storeDirectoryStats;
try {
storeDirectoryStats = await fsPromises.lstat(storeDirectoryWithContainer);
} catch (ex) {
if (ex.code === 'ENOENT') {
debugController(`Container ${container.name} does not exist locally (${storeDirectoryWithContainer}), will not sync this container, create the folder if you want to sync it.`);
} else {
debugController(`Unable to load local container ${container.name} directory.`, ex);
}
return;
}
const files = await fsPromises.readdir(storeDirectoryWithContainer);
const filesInLocalContainer = files.length;
debugController(`${storeDirectoryStats.isSymbolicLink() ? 'Symbolic ' : ''}Container ${container.name} exists , it contains ${filesInLocalContainer} files, and will now sync...`);
const containerURL = Azure.ContainerURL.fromServiceURL(serviceURL, container.name);
let marker;
let localCopiesAlreadyExisted = 0;
let filesCopied = 0;
let filesFailed = 0;
do {
const listBlobsResponse = await containerURL.listBlobFlatSegment(
Azure.Aborter.none,
marker
);
marker = listBlobsResponse.nextMarker;
for (const blob of listBlobsResponse.segment.blobItems) {
let localFilePath = `${storeDirectoryWithContainer}${blob.name}`;
try {
await fsPromises.access(localFilePath);
localCopiesAlreadyExisted += 1;
continue;
} catch (err) {
if (err.code !== 'ENOENT') {
// Swallow the 'does not exist' error, since we will create it.
// otherwise we skip it.
debugFile(`${blob.name} > ${localFilePath} - ERROR: `, err);
filesFailed += 1;
continue;
}
}
debugFile(`${blob.name} > ${localFilePath} - TODO`);
try {
const blobURL = Azure.BlobURL.fromContainerURL(containerURL, blob.name);
const blockBlobURL = Azure.BlockBlobURL.fromBlobURL(blobURL);
const downloadBlockBlobResponse = await blockBlobURL.download(Azure.Aborter.none, 0);
const fileStream = fs.createWriteStream(localFilePath);
await new Promise((writeFinished, writeFailed) => {
const stream = downloadBlockBlobResponse.readableStreamBody.pipe(fileStream);
stream.on('finish', () =>{
writeFinished();
});
});
} catch (err) {
debugFile(`${blob.name} > ${localFilePath} - FAILED`, err);
filesFailed += 1;
continue;
}
filesCopied += 1;
debugFile(`${blob.name} > ${localFilePath} - COMPLETE - SUCCESS`);
}
} while (marker);
debugDirectory(`${localCopiesAlreadyExisted} local files already existed for ${container.name}, ${filesCopied} files copied, ${filesFailed} files failed.`);
}