( options: AzureMultipartUploadInit )
| 496 | * Initiate a multipart upload for Azure Blob Storage |
| 497 | */ |
| 498 | export async function initiateMultipartUpload( |
| 499 | options: AzureMultipartUploadInit |
| 500 | ): Promise<{ uploadId: string; key: string }> { |
| 501 | const { BlobServiceClient, StorageSharedKeyCredential } = await import('@azure/storage-blob') |
| 502 | const { fileName, contentType, customConfig, customKey } = options |
| 503 | |
| 504 | let blobServiceClient: BlobServiceClientType |
| 505 | let containerName: string |
| 506 | |
| 507 | if (customConfig) { |
| 508 | if (customConfig.connectionString) { |
| 509 | blobServiceClient = BlobServiceClient.fromConnectionString(customConfig.connectionString) |
| 510 | } else if (customConfig.accountName && customConfig.accountKey) { |
| 511 | const credential = new StorageSharedKeyCredential( |
| 512 | customConfig.accountName, |
| 513 | customConfig.accountKey |
| 514 | ) |
| 515 | blobServiceClient = new BlobServiceClient( |
| 516 | `https://${customConfig.accountName}.blob.core.windows.net`, |
| 517 | credential |
| 518 | ) |
| 519 | } else { |
| 520 | throw new Error('Invalid custom blob configuration') |
| 521 | } |
| 522 | containerName = customConfig.containerName |
| 523 | } else { |
| 524 | blobServiceClient = await getBlobServiceClient() |
| 525 | containerName = BLOB_CONFIG.containerName |
| 526 | } |
| 527 | |
| 528 | const safeFileName = sanitizeFileName(fileName) |
| 529 | const uniqueKey = customKey || `kb/${generateId()}-${safeFileName}` |
| 530 | |
| 531 | const uploadId = generateId() |
| 532 | |
| 533 | const containerClient = blobServiceClient.getContainerClient(containerName) |
| 534 | const blockBlobClient = containerClient.getBlockBlobClient(uniqueKey) |
| 535 | |
| 536 | await blockBlobClient.setMetadata({ |
| 537 | uploadId, |
| 538 | fileName: encodeURIComponent(fileName), |
| 539 | contentType, |
| 540 | uploadStarted: new Date().toISOString(), |
| 541 | multipartUpload: 'true', |
| 542 | }) |
| 543 | |
| 544 | return { |
| 545 | uploadId, |
| 546 | key: uniqueKey, |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Generate presigned URLs for uploading parts |
no test coverage detected