-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.ts
72 lines (61 loc) · 2.4 KB
/
storage.ts
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
"use strict";
import { BlobServiceClient } from "@azure/storage-blob";
import "dotenv/config"
// This function creates client and perfroms authentication
async function getBlobServiceClient(): Promise<BlobServiceClient> {
console.log("\nAuthenticating credentials for Azure Storage Account and creating a service client");
const AZURE_STORAGE_CONNECTION_STRING =
process.env.AZURE_STORAGE_CONNECTION_STRING;
if (!AZURE_STORAGE_CONNECTION_STRING) {
throw Error('Azure Storage Connection string not found');
}
// Create the BlobServiceClient object with connection string
const blobServiceClient = BlobServiceClient.fromConnectionString(
AZURE_STORAGE_CONNECTION_STRING
);
return blobServiceClient
}
// Function to conver steam into a buffer
// This buffer is converted to string and pinted
async function streamToBuffer(stream: NodeJS.ReadableStream): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
stream.on("data", (data) => {
chunks.push(Buffer.isBuffer(data) ? data : Buffer.from(data));
});
stream.on("end", () => {
resolve(Buffer.concat(chunks));
});
stream.on("error", reject);
});
}
async function getBlobDataAsString(blobName: string): Promise<void> {
const blobServiceClient = await getBlobServiceClient();
console.log("\nBlob Content");
const CONTAINER_NAME = process.env.CONTAINER_NAME;
if (!CONTAINER_NAME) {
throw Error('Azure Storage Container name not found');
}
const containerClient = blobServiceClient.getContainerClient(CONTAINER_NAME);
let blockBlobClient = containerClient.getBlockBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download();
console.log(
`Downloaded blob content: \n ${(
await streamToBuffer(downloadBlockBlobResponse.readableStreamBody!)
).toString()},`
);
console.log(
`requestId - ${downloadBlockBlobResponse.requestId}, statusCode - ${downloadBlockBlobResponse._response.status}\n`
);
}
async function listBlobsInContainer(): Promise<void> {
console.log("\nList of Blobs");
//TODO: Print the list blobs which are present in Azure storage container
// Container name can be accessed from the env
const CONTAINER_NAME = process.env.CONTAINER_NAME;
if (!CONTAINER_NAME) {
throw Error('Azure Storage Container name not found');
}
}
getBlobDataAsString("2022-12-22-09-00.txt");
// listBlobsInContainer();