Skip to content

Commit

Permalink
Fixed parseAzureBlobUrl method
Browse files Browse the repository at this point in the history
Changed method for getting blob service client
  • Loading branch information
hassansuhaib committed Sep 18, 2023
1 parent 701f41c commit 81c0900
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/driver-azure/src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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,
}
}

Expand All @@ -41,9 +49,14 @@ async function streamToBuffer(readableStream: NodeJS.ReadableStream) {
}

export const azureStorageBlobFs = async (azureBlobUrl: string): Promise<VirtualFS> => {
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)
Expand Down

0 comments on commit 81c0900

Please sign in to comment.