import { v4 as uuidv4 } from "uuid"; import { BlobServiceClient, ContainerClient } from "@azure/storage-blob"; import { DefaultAzureCredential, InteractiveBrowserCredential, EnvironmentCredential, ClientSecretCredential, } from "@azure/identity"; import { consoleLogger, hashAPIPath } from "."; import _ from "lodash"; const STORAGE_PATH = process.env.AZURE_PEDW_STORAGE_ENDPOINT; const STORAGE_CONTAINER = process.env.AZURE_PEDW_CONTAINER; export const createContainer = async (containerName) => { const creds = new DefaultAzureCredential(); containerName = containerName.toLowerCase(); // console.log( // "container name:", // containerName, // STORAGE_PATH + "/" + containerName // ); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); const blobServiceClient = new BlobServiceClient(`${STORAGE_PATH}`, creds); const createContainerResponse = await containerClient.createIfNotExists(); // console.log( // `Created container ${containerName} successfully`, // createContainerResponse.requestId // ); // console.log("Containers:"); // for await (const container of blobServiceClient.listContainers()) { // console.log(`- ${container.name}`); // } return containerName; }; export const getContainers = async () => { const creds = new DefaultAzureCredential(); const blobServiceClient = new BlobServiceClient(`${STORAGE_PATH}`, creds); console.log("Containers:"); for await (const container of blobServiceClient.listContainers()) { console.log(`- ${container.name}`); } }; export const getBlobs = async (containerName) => { const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); containerClient.createIfNotExists(); const blobObj = []; for await (const blob of containerClient.listBlobsFlat({ prefix: "files/", })) { let blobDocumentType = blob.name .split("/")[1] .slice(0, blob.name.split("/")[1].indexOf("_")); blobObj.push({ "name": blob.name.split("/")[1], "path": blob.name, "documentType": blobDocumentType, "versionId": blob.versionId, "isCurrentVersion": blob.isCurrentVersion, "contentLength": blob.properties.contentLength, "contentType": blob.contentType, "lastModified": blob.properties.lastModified, "hashedfilepath": hashAPIPath( "/api/file/downloadblob?container=" + containerName.toLowerCase() + "&blobname=" + blob.name.split("/")[1] ), "hasheddeletepath": hashAPIPath( "/api/file/deleteblob?container=" + containerName.toLowerCase() + "&blobname=" + blob.name.split("/")[1] ), "hashgetblobs": hashAPIPath( "/api/file/getbloblist?container=" + containerName.toLowerCase() ), }); } return blobObj; }; export const createBlob = async (formContent, containerName) => { const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); const content = JSON.stringify(formContent); const blobName = formContent.pinswg_name + "_" + new Date().getTime() + ".json"; const blockBlobClient = containerClient.getBlockBlobClient(blobName); const uploadBlobResponse = await blockBlobClient.upload( content, Buffer.byteLength(content) ); // console.log("Blobs:"); // for await (const blob of containerClient.listBlobsByHierarchy("/")) { // console.log(`- ${blob.name}`); // } return formContent.pinswg_name; }; export const deleteBlob = async (containerName, blobName) => { const creds = new DefaultAzureCredential(); const options = { deleteSnapshots: "include", // or 'only' }; const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName.toLowerCase()}`, creds ); const blockBlobClient = containerClient.getBlockBlobClient(blobName); await blockBlobClient.deleteIfExists(options); console.log(`deleted blob ${blobName}`); return { "deleted": blobName }; }; export const uploadFile = async (formContent, containerName, foldername) => { const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); const files = formContent; //console.log(...formContent); //console.log("files...", files, files.length); for (const prop in files) { //console.log(`files[${prop}] = ${files[prop][0].size}`); const blobName = "files/" + files[prop][0].fieldName; const blockBlobClient = containerClient.getBlockBlobClient(blobName); const uploadBlobResponse = await blockBlobClient.uploadFile( files[prop][0].path, files[prop].size ); console.log( `Uploaded block blob ${files[prop][0].fieldName} successfully`, uploadBlobResponse.requestId ); } }; export const downloadFile = async (containerName, blobName) => { //console.log.apply(containerName, blobName); const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); const blobClient = containerClient.getBlobClient("files/" + blobName); const downloadedBlob = await blobClient.download(0); const downloaded = await streamToBuffer(downloadedBlob.readableStreamBody); return downloaded; }; export const downloadProgressFile = async (containerName, blobName) => { //console.log.apply(containerName, blobName); const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); const blobClient = containerClient.getBlobClient(blobName); const downloadedBlob = await blobClient.download(0); const downloaded = await streamToBuffer(downloadedBlob.readableStreamBody); return downloaded; }; const streamToBuffer = async (readableStream) => { return new Promise((resolve, reject) => { const chunks = []; readableStream.on("data", (data) => { chunks.push(data instanceof Buffer ? data : Buffer.from(data)); }); readableStream.on("end", () => { resolve(Buffer.concat(chunks)); }); readableStream.on("error", reject); }); }; export const getProgressBlobs = async (containerName) => { const creds = new DefaultAzureCredential(); const containerClient = new ContainerClient( `${STORAGE_PATH}/${containerName}`, creds ); containerClient.createIfNotExists(); let blobObj = []; for await (const blob of containerClient.listBlobsFlat()) { blobObj.push({ "name": blob.name.split("/")[1], "path": blob.name, "versionId": blob.versionId, "isCurrentVersion": blob.isCurrentVersion, "contentLength": blob.properties.contentLength, "contentType": blob.contentType, "lastModified": blob.properties.lastModified, "hashedfilepath": hashAPIPath( "/api/file/downloadblob?container=" + containerName.toLowerCase() + "&blobname=" + blob.name.split("/")[1] ), "hasheddeletepath": hashAPIPath( "/api/file/deleteblob?container=" + containerName.toLowerCase() + "&blobname=" + blob.name.split("/")[1] ), "hashgetblobs": hashAPIPath( "/api/file/getbloblist?container=" + containerName.toLowerCase() ), }); } blobObj = _.sortBy(blobObj, [ function (o) { return o.lastModified; }, ]).reverse()[0]; console.log(blobObj.path); return blobObj; };