( key: string, customConfig?: BlobConfig )
| 390 | * Returns null when the blob is missing. |
| 391 | */ |
| 392 | export async function headBlobObject( |
| 393 | key: string, |
| 394 | customConfig?: BlobConfig |
| 395 | ): Promise<{ size: number; contentType?: string } | null> { |
| 396 | const { BlobServiceClient, StorageSharedKeyCredential } = await import('@azure/storage-blob') |
| 397 | let blobServiceClient: BlobServiceClientType |
| 398 | let containerName: string |
| 399 | |
| 400 | if (customConfig) { |
| 401 | if (customConfig.connectionString) { |
| 402 | blobServiceClient = BlobServiceClient.fromConnectionString(customConfig.connectionString) |
| 403 | } else if (customConfig.accountName && customConfig.accountKey) { |
| 404 | const credential = new StorageSharedKeyCredential( |
| 405 | customConfig.accountName, |
| 406 | customConfig.accountKey |
| 407 | ) |
| 408 | blobServiceClient = new BlobServiceClient( |
| 409 | `https://${customConfig.accountName}.blob.core.windows.net`, |
| 410 | credential |
| 411 | ) |
| 412 | } else { |
| 413 | throw new Error('Invalid custom blob configuration') |
| 414 | } |
| 415 | containerName = customConfig.containerName |
| 416 | } else { |
| 417 | blobServiceClient = await getBlobServiceClient() |
| 418 | containerName = BLOB_CONFIG.containerName |
| 419 | } |
| 420 | |
| 421 | const containerClient = blobServiceClient.getContainerClient(containerName) |
| 422 | const blockBlobClient = containerClient.getBlockBlobClient(key) |
| 423 | |
| 424 | try { |
| 425 | const properties = await blockBlobClient.getProperties() |
| 426 | return { |
| 427 | size: properties.contentLength ?? 0, |
| 428 | contentType: properties.contentType, |
| 429 | } |
| 430 | } catch (err) { |
| 431 | const status = (err as { statusCode?: number }).statusCode |
| 432 | const code = (err as { code?: string }).code |
| 433 | if (status === 404 || code === 'BlobNotFound') { |
| 434 | return null |
| 435 | } |
| 436 | throw err |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Delete a file from Azure Blob Storage |
no test coverage detected