diff --git a/actions/azurestorage.js b/actions/azurestorage.js index 67dae4d6..06f5593e 100644 --- a/actions/azurestorage.js +++ b/actions/azurestorage.js @@ -34,6 +34,8 @@ const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME; export const createContainerSas = async (containerName) => { // Get environment variables + containerName.length > 0; + // Best practice: create time limits const TEN_MINUTES = 10 * 60 * 1000; const NOW = new Date(); @@ -68,7 +70,7 @@ export const createContainerSas = async (containerName) => { expiresOn: TEN_MINUTES_AFTER_NOW, }; - //conLogJSON.stringify(sasOptions)); + console.log(JSON.stringify(sasOptions)); const sasToken = generateBlobSASQueryParameters( sasOptions, @@ -1116,12 +1118,13 @@ export const getAllProgressBlobs = async (containerName) => { }); } - //console.log( - // "\n////////////////////////////\n", - // "blobObj:", - // blobObj, - // "\n////////////////////////////" - // ); + console.log( + "\n////////////////////////////\n", + sasUrl, + "blobObj:", + blobObj, + "\n////////////////////////////" + ); return blobObj; }; @@ -1332,3 +1335,84 @@ export const createRepCompleteMessage = async ( return sendMessageResponse; }; + +export const listContainers = async ( + blobServiceClient, + containerNamePrefix +) => { + const options = { + includeDeleted: false, + includeMetadata: true, + includeSystem: true, + prefix: containerNamePrefix, + }; + console.log( + "\n\n\n=================================================\n", + "Showing storage account contents:\n\n" + ); + for await (const containerItem of blobServiceClient.listContainers( + options + )) { + // ContainerItem + console.log(`For-await list: ${containerItem.name}`); + + // ContainerClient + const containerClient = blobServiceClient.getContainerClient( + containerItem.name + ); + + await listBlobHierarchical(containerClient).catch((error) => + consoleLogger(error) + ); + // ... do something with container + } + console.log("\n=================================================\n\n\n\n"); +}; + +export const listBlobHierarchical = async ( + containerClient, + virtualHierarchyDelimiter = "/" +) => { + // page size - artificially low as example + const maxPageSize = 2; + + // some options for filtering list + const listOptions = { + includeCopy: false, // include metadata from previous copies + includeDeleted: false, // include deleted blobs + includeDeletedWithVersions: false, // include deleted blobs with versions + includeLegalHold: false, // include legal hold + includeMetadata: true, // include custom metadata + includeSnapshots: false, // include snapshots + includeTags: true, // include indexable tags + includeUncommitedBlobs: false, // include uncommitted blobs + includeVersions: false, // include all blob version + prefix: "", // filter by blob name prefix + }; + + let i = 1; + console.log(`Folder ${virtualHierarchyDelimiter}`); + + for await (const response of containerClient + .listBlobsByHierarchy(virtualHierarchyDelimiter, listOptions) + .byPage({ maxPageSize })) { + console.log(` Blob number ${i++}`); + const segment = response.segment; + + if (segment.blobPrefixes) { + // Do something with each virtual folder + for await (const prefix of segment.blobPrefixes) { + // build new virtualHierarchyDelimiter from current and next + await listBlobHierarchical( + containerClient, + `${virtualHierarchyDelimiter}${prefix.name}` + ); + } + } + + for (const blob of response.segment.blobItems) { + // Do something with each blob + console.log(`\tBlobItem: name - ${blob.name}`); + } + } +}; diff --git a/pages/dbcheck.js b/pages/dbcheck.js new file mode 100644 index 00000000..95f55d34 --- /dev/null +++ b/pages/dbcheck.js @@ -0,0 +1,74 @@ +import { DefaultAzureCredential } from "@azure/identity"; +import { BlobServiceClient, ContainerClient } from "@azure/storage-blob"; +import { v4 as uuidv4 } from "uuid"; +import { + createContainerSas, + listContainers, + listBlobHierarchical, +} from "../actions/azurestorage"; + +import { PrismaClient } from "@prisma/client"; + +import { consoleLogger, getIP } from "../actions"; + +const ShowStorage = (props) => { + const { container } = props; + + return
hello
; +}; + +export const getServerSideProps = async (ctx) => { + const { query, req, res } = ctx; + getIP(req); + + console.log( + "have these?:", + process.env.AZURE_TENANT_ID, + process.env.AZURE_CLIENT_ID, + process.env.AZURE_CLIENT_SECRET + ); + const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME; + const showDebug = process.env.SHOW_DEBUG || false; + + const creds = new DefaultAzureCredential(); + + const blobServiceClient = new BlobServiceClient( + `https://${accountName}.blob.core.windows.net`, + creds + ); + + console.log( + "\n\n\n=================================================\n", + "Environment name:" + accountName, + "\n=================================================\n" + ); + + const prisma = new PrismaClient(); + + async function checkDB() { + const allUsers = await prisma.user.findMany(); + console.log( + "\n\n\n=================================================\n", + "Show users in Auth DB:\n", + allUsers, + "\n=================================================\n" + ); + } + + showDebug && + (checkDB() + .then(async () => { + await prisma.$disconnect(); + }) + .catch(async (e) => { + consoleLogger(e); + await prisma.$disconnect(); + //process.exit(1); + }), + await listContainers(blobServiceClient).catch((error) => + consoleLogger(error) + )); + return { props: {} }; +}; + +export default ShowStorage;