( key: string, customConfig?: BlobConfig )
| 347 | * `destroy()` the returned stream. Used by the large-CSV import worker. |
| 348 | */ |
| 349 | export async function downloadFromBlobStream( |
| 350 | key: string, |
| 351 | customConfig?: BlobConfig |
| 352 | ): Promise<Readable> { |
| 353 | const { BlobServiceClient, StorageSharedKeyCredential } = await import('@azure/storage-blob') |
| 354 | let blobServiceClient: BlobServiceClientType |
| 355 | let containerName: string |
| 356 | |
| 357 | if (customConfig) { |
| 358 | if (customConfig.connectionString) { |
| 359 | blobServiceClient = BlobServiceClient.fromConnectionString(customConfig.connectionString) |
| 360 | } else if (customConfig.accountName && customConfig.accountKey) { |
| 361 | const credential = new StorageSharedKeyCredential( |
| 362 | customConfig.accountName, |
| 363 | customConfig.accountKey |
| 364 | ) |
| 365 | blobServiceClient = new BlobServiceClient( |
| 366 | `https://${customConfig.accountName}.blob.core.windows.net`, |
| 367 | credential |
| 368 | ) |
| 369 | } else { |
| 370 | throw new Error('Invalid custom blob configuration') |
| 371 | } |
| 372 | containerName = customConfig.containerName |
| 373 | } else { |
| 374 | blobServiceClient = await getBlobServiceClient() |
| 375 | containerName = BLOB_CONFIG.containerName |
| 376 | } |
| 377 | |
| 378 | const containerClient = blobServiceClient.getContainerClient(containerName) |
| 379 | const blockBlobClient = containerClient.getBlockBlobClient(key) |
| 380 | |
| 381 | const downloadBlockBlobResponse = await blockBlobClient.download() |
| 382 | if (!downloadBlockBlobResponse.readableStreamBody) { |
| 383 | throw new Error('Failed to get readable stream from blob download') |
| 384 | } |
| 385 | return downloadBlockBlobResponse.readableStreamBody as Readable |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Check whether a blob exists (and return its size when it does). |
no test coverage detected