* Generate presigned URL for S3
(
key: string,
contentType: string,
fileSize: number,
metadata: Record<string, string>,
config: { bucket?: string; region?: string },
expirationSeconds: number
)
| 584 | * Generate presigned URL for S3 |
| 585 | */ |
| 586 | async function generateS3PresignedUrl( |
| 587 | key: string, |
| 588 | contentType: string, |
| 589 | fileSize: number, |
| 590 | metadata: Record<string, string>, |
| 591 | config: { bucket?: string; region?: string }, |
| 592 | expirationSeconds: number |
| 593 | ): Promise<PresignedUrlResponse> { |
| 594 | const { getS3Client } = await import('@/lib/uploads/providers/s3/client') |
| 595 | const { PutObjectCommand } = await import('@aws-sdk/client-s3') |
| 596 | const { getSignedUrl } = await import('@aws-sdk/s3-request-presigner') |
| 597 | |
| 598 | if (!config.bucket || !config.region) { |
| 599 | throw new Error('S3 configuration missing bucket or region') |
| 600 | } |
| 601 | |
| 602 | const sanitizedMetadata = sanitizeStorageMetadata(metadata, 2000) |
| 603 | if (sanitizedMetadata.originalName) { |
| 604 | sanitizedMetadata.originalName = sanitizeFilenameForMetadata(sanitizedMetadata.originalName) |
| 605 | } |
| 606 | |
| 607 | const command = new PutObjectCommand({ |
| 608 | Bucket: config.bucket, |
| 609 | Key: key, |
| 610 | ContentType: contentType, |
| 611 | ContentLength: fileSize, |
| 612 | Metadata: sanitizedMetadata, |
| 613 | }) |
| 614 | |
| 615 | const presignedUrl = await getSignedUrl(getS3Client(), command, { expiresIn: expirationSeconds }) |
| 616 | |
| 617 | return { |
| 618 | url: presignedUrl, |
| 619 | key, |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Generate presigned URL for Azure Blob |
no test coverage detected