diff options
author | James Hu <51237606+jameshu111@users.noreply.github.com> | 2023-06-26 10:37:11 -0400 |
---|---|---|
committer | James Hu <51237606+jameshu111@users.noreply.github.com> | 2023-06-26 10:37:11 -0400 |
commit | 5f44a6cf1f16023a4c39872f2ccfc129c65ea812 (patch) | |
tree | a90a3b8bf986cf8ea8a67016b9ceb5e01d3841c2 /src/server/ApiManagers/AzureManager.ts | |
parent | 1bc15f9b408d35639cea37ea1c29e7cdbd326301 (diff) |
temp working version
Diffstat (limited to 'src/server/ApiManagers/AzureManager.ts')
-rw-r--r-- | src/server/ApiManagers/AzureManager.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/server/ApiManagers/AzureManager.ts b/src/server/ApiManagers/AzureManager.ts new file mode 100644 index 000000000..e105f5d80 --- /dev/null +++ b/src/server/ApiManagers/AzureManager.ts @@ -0,0 +1,74 @@ +import { ContainerClient, BlobServiceClient } from "@azure/storage-blob"; +// import * as dotenv from 'dotenv'; +import * as fs from "fs"; +import { Readable, Stream } from "stream"; +// dotenv.config(); +const AZURE_STORAGE_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=dashblobstore;AccountKey=3i+E5XkCz3TJ0m5QOatiEnbRACz9V1qCW72L6ldiYGH1tLdfJWa2eQoRfYmPA68lx1a6YAcfYJfWHadIxQvhGQ==;EndpointSuffix=core.windows.net"; + +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"; + + constructor() { + if (!AZURE_STORAGE_CONNECTION_STRING) { + throw Error("Azure Storage Connection String Not Found"); + } + // this._blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING); + 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) { + console.log("Upload Blob File"); + 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 UploadBlobStream(stream: Readable, filename: string, filetype: string) { + console.log("Upload Blob Stream: %s, %s", filename, filetype); + const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename); + const blobOptions = { blobHTTPHeaders: { blobContentType: filetype }}; + return blockBlobClient.uploadStream(stream, undefined, undefined, blobOptions); + } + + public static DeleteBlob(filename: string) { + console.log("Delete Blob from filename"); + const blockBlobClient = this.Instance.ContainerClient.getBlockBlobClient(filename); + return blockBlobClient.deleteIfExists(); + } + + public static async GetBlobs() { + console.log("Get Blobs"); + 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; + } +}
\ No newline at end of file |