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
|
import { ContainerClient, BlobServiceClient } from "@azure/storage-blob";
import * as fs from "fs";
import { Readable, Stream } from "stream";
import * as path from "path";
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
const extToType: { [suffix: string]: string } = {
".jpeg" : "image/jpeg",
".jpg" : "image/jpeg",
".png" : "image/png",
".svg" : "image/svg+xml",
".webp" : "image/webp",
".gif" : "image/gif"
}
export class AzureManager {
private _containerClient: ContainerClient;
private _blobServiceClient: BlobServiceClient;
private static _instance: AzureManager | undefined;
public static CONTAINER_NAME = "dashmedia";
public static STORAGE_ACCOUNT_NAME = "dashblobstore";
public static BASE_STRING = `https://${AzureManager.STORAGE_ACCOUNT_NAME}.blob.core.windows.net/${AzureManager.CONTAINER_NAME}`;
constructor() {
if (!AZURE_STORAGE_CONNECTION_STRING) {
throw new Error("Azure Storage Connection String Not Found");
}
this._blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
this._containerClient = this.BlobServiceClient.getContainerClient(AzureManager.CONTAINER_NAME);
}
public static get Instance() {
return this._instance = this._instance ?? new AzureManager();
}
public get BlobServiceClient() {
return this._blobServiceClient;
}
public get ContainerClient() {
return this._containerClient;
}
public static UploadBlob(filename: string, filepath: string, filetype: string) {
const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename);
const blobOptions = { blobHTTPHeaders: { blobContentType: filetype }};
const stream = fs.createReadStream(filepath);
return blockBlobClient.uploadStream(stream, undefined, undefined, blobOptions);
}
public static UploadBase64ImageBlob(filename: string, data: string, filetype?: string) {
const confirmedFiletype = filetype ? filetype : extToType[path.extname(filename)];
const buffer = Buffer.from(data, "base64");
const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename);
const blobOptions = { blobHTTPHeaders: { blobContentType: confirmedFiletype } };
return blockBlobClient.upload(buffer, buffer.length, blobOptions);
}
public static UploadBlobStream(stream: Readable, filename: string, filetype: string) {
const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename);
const blobOptions = { blobHTTPHeaders: { blobContentType: filetype }};
return blockBlobClient.uploadStream(stream, undefined, undefined, blobOptions);
}
public static DeleteBlob(filename: string) {
const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename);
return blockBlobClient.deleteIfExists();
}
public static async GetBlobs() {
const foundBlobs = [];
for await (const blob of this.Instance.ContainerClient.listBlobsFlat()) {
console.log(`${blob.name}`);
const blobItem = {
url : `https://${AzureManager.STORAGE_ACCOUNT_NAME}.blob.core.windows.net/${AzureManager.CONTAINER_NAME}/${blob.name}`,
name : blob.name
}
foundBlobs.push(blobItem);
}
return foundBlobs;
}
}
|