Skip to content

Commit aac3f63

Browse files
fix: Download file method would break due to missing file destination
* fix: creates files folder if it doesn't exist * generalize logic for folder creation * add more explanation to the downloadFile method --------- Co-authored-by: Antoine Hurard <antoine.reliefapps@gmail.com>
1 parent bf3d1bb commit aac3f63

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/utils/files/downloadFile.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BlobServiceClient } from '@azure/storage-blob';
22
import { logger } from '@services/logger.service';
33
import config from 'config';
4+
import fs from 'fs';
45

56
/** Azure storage connection string */
67
const AZURE_STORAGE_CONNECTION_STRING: string = config.get(
@@ -20,12 +21,22 @@ export const downloadFile = async (
2021
blobName: string,
2122
path: string
2223
): Promise<void> => {
24+
// Azure Blob Storage API : Create the blob client
2325
const blobServiceClient = BlobServiceClient.fromConnectionString(
2426
AZURE_STORAGE_CONNECTION_STRING
2527
);
2628
const containerClient = blobServiceClient.getContainerClient(containerName);
2729
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
2830
try {
31+
// If path does not exist, create it
32+
const pathArr = path.split('/');
33+
pathArr.pop();
34+
const pathToFile = pathArr.join('/');
35+
36+
if (!fs.existsSync(pathToFile))
37+
fs.mkdirSync(pathToFile, { recursive: true });
38+
39+
// Download the file
2940
await blockBlobClient.downloadToFile(path);
3041
} catch (err) {
3142
logger.error(err.message);

0 commit comments

Comments
 (0)