| import { logger } from '@librechat/data-schemas'; |
| import { DefaultAzureCredential } from '@azure/identity'; |
| import type { ContainerClient, BlobServiceClient } from '@azure/storage-blob'; |
|
|
| let blobServiceClient: BlobServiceClient | null = null; |
| let azureWarningLogged = false; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export const initializeAzureBlobService = async (): Promise<BlobServiceClient | null> => { |
| if (blobServiceClient) { |
| return blobServiceClient; |
| } |
| const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING; |
| if (connectionString) { |
| const { BlobServiceClient } = await import('@azure/storage-blob'); |
| blobServiceClient = BlobServiceClient.fromConnectionString(connectionString); |
| logger.info('Azure Blob Service initialized using connection string'); |
| } else { |
| const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME; |
| if (!accountName) { |
| if (!azureWarningLogged) { |
| logger.error( |
| '[initializeAzureBlobService] Azure Blob Service not initialized. Connection string missing and AZURE_STORAGE_ACCOUNT_NAME not provided.', |
| ); |
| azureWarningLogged = true; |
| } |
| return null; |
| } |
| const url = `https://${accountName}.blob.core.windows.net`; |
| const credential = new DefaultAzureCredential(); |
| const { BlobServiceClient } = await import('@azure/storage-blob'); |
| blobServiceClient = new BlobServiceClient(url, credential); |
| logger.info('Azure Blob Service initialized using Managed Identity'); |
| } |
| return blobServiceClient; |
| }; |
|
|
| |
| |
| |
| |
| |
| export const getAzureContainerClient = async ( |
| containerName = process.env.AZURE_CONTAINER_NAME || 'files', |
| ): Promise<ContainerClient | null> => { |
| const serviceClient = await initializeAzureBlobService(); |
| return serviceClient ? serviceClient.getContainerClient(containerName) : null; |
| }; |
|
|