From 426c37ca0ac0b269fe629e298220a759066c1083 Mon Sep 17 00:00:00 2001 From: hassansuhaib Date: Mon, 18 Sep 2023 12:49:26 +0500 Subject: [PATCH] Fixed parseAzureBlobUrl method Changed method for getting blob service client --- packages/driver-azure/src/fs/index.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/driver-azure/src/fs/index.ts b/packages/driver-azure/src/fs/index.ts index 947d6dfb..a80f3b2c 100644 --- a/packages/driver-azure/src/fs/index.ts +++ b/packages/driver-azure/src/fs/index.ts @@ -1,4 +1,5 @@ import { BlobServiceClient, ContainerClient, BlobDeleteOptions, BlobDeleteIfExistsResponse } from '@azure/storage-blob' +import { DefaultAzureCredential } from '@azure/identity' import { VirtualFS } from '@preevy/core' export const defaultBucketName = ( @@ -17,13 +18,20 @@ const ensureBucketExists = async (blobServiceClient: BlobServiceClient, containe export const parseAzureBlobUrl = (azureBlobUrl: string) => { const url = new URL(azureBlobUrl) - if (url.protocol !== 'azblob:') { - throw new Error('Azure Blob urls must start with azblob://') + + if (!(url.protocol === 'https:')) { + throw new Error('Azure Blob urls must start with https:') } + + const accountName = url.hostname.split('.')[0] + + const pathSegments = url.pathname.split('/') + const containerName = pathSegments[1] + return { url, - containerName: url.hostname, - path: url.pathname, + accountName, + containerName, } } @@ -41,9 +49,14 @@ async function streamToBuffer(readableStream: NodeJS.ReadableStream) { } export const azureStorageBlobFs = async (azureBlobUrl: string): Promise => { - const { path, containerName } = parseAzureBlobUrl(azureBlobUrl) + const { accountName, containerName } = parseAzureBlobUrl(azureBlobUrl) + + const defaultAzureCredential = new DefaultAzureCredential() - const blobServiceClient = BlobServiceClient.fromConnectionString(path) + const blobServiceClient = new BlobServiceClient( + `https://${accountName}.blob.core.windows.net`, + defaultAzureCredential + ) const containerClient = blobServiceClient.getContainerClient(containerName) await ensureBucketExists(blobServiceClient, containerName)