( key: string, customConfig?: BlobConfig, maxBytes?: number )
| 279 | ): Promise<Buffer> |
| 280 | |
| 281 | export async function downloadFromBlob( |
| 282 | key: string, |
| 283 | customConfig?: BlobConfig, |
| 284 | maxBytes?: number |
| 285 | ): Promise<Buffer> { |
| 286 | const { BlobServiceClient, StorageSharedKeyCredential } = await import('@azure/storage-blob') |
| 287 | let blobServiceClient: BlobServiceClientType |
| 288 | let containerName: string |
| 289 | |
| 290 | if (customConfig) { |
| 291 | if (customConfig.connectionString) { |
| 292 | blobServiceClient = BlobServiceClient.fromConnectionString(customConfig.connectionString) |
| 293 | } else if (customConfig.accountName && customConfig.accountKey) { |
| 294 | const credential = new StorageSharedKeyCredential( |
| 295 | customConfig.accountName, |
| 296 | customConfig.accountKey |
| 297 | ) |
| 298 | blobServiceClient = new BlobServiceClient( |
| 299 | `https://${customConfig.accountName}.blob.core.windows.net`, |
| 300 | credential |
| 301 | ) |
| 302 | } else { |
| 303 | throw new Error('Invalid custom blob configuration') |
| 304 | } |
| 305 | containerName = customConfig.containerName |
| 306 | } else { |
| 307 | blobServiceClient = await getBlobServiceClient() |
| 308 | containerName = BLOB_CONFIG.containerName |
| 309 | } |
| 310 | |
| 311 | const containerClient = blobServiceClient.getContainerClient(containerName) |
| 312 | const blockBlobClient = containerClient.getBlockBlobClient(key) |
| 313 | |
| 314 | const downloadBlockBlobResponse = await blockBlobClient.download() |
| 315 | if (maxBytes !== undefined && downloadBlockBlobResponse.contentLength !== undefined) { |
| 316 | try { |
| 317 | assertKnownSizeWithinLimit( |
| 318 | downloadBlockBlobResponse.contentLength, |
| 319 | maxBytes, |
| 320 | 'storage download' |
| 321 | ) |
| 322 | } catch (error) { |
| 323 | const stream = downloadBlockBlobResponse.readableStreamBody as |
| 324 | | { destroy?: (error?: Error) => void } |
| 325 | | undefined |
| 326 | stream?.destroy?.(error instanceof Error ? error : undefined) |
| 327 | throw error |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (!downloadBlockBlobResponse.readableStreamBody) { |
| 332 | throw new Error('Failed to get readable stream from blob download') |
| 333 | } |
| 334 | const downloaded = await readNodeStreamToBufferWithLimit( |
| 335 | downloadBlockBlobResponse.readableStreamBody, |
| 336 | { |
| 337 | maxBytes: maxBytes ?? Number.MAX_SAFE_INTEGER, |
| 338 | label: 'storage download', |
no test coverage detected