{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "generate-presigned-download-url",
  "type": "registry:lib",
  "title": "Generate Presigned Download URL",
  "description": "Generate a presigned download URL for a file.",
  "dependencies": [
    "@aws-sdk/client-s3",
    "@aws-sdk/s3-request-presigner"
  ],
  "files": [
    {
      "path": "registry/default/storage/generate-presigned-download-url.ts",
      "content": "import { GetObjectCommand } from '@aws-sdk/client-s3';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\n\nimport { getS3Client } from '@/registry/default/storage/s3-client';\n\nexport async function generatePresignedDownloadUrl(\n  key: string,\n  expiresIn = 3600,\n) {\n  try {\n    const command = new GetObjectCommand({\n      Bucket: process.env.S3_BUCKET_NAME,\n      Key: key,\n    });\n\n    const url = await getSignedUrl(getS3Client(), command, { expiresIn });\n    return url;\n  } catch (err) {\n    console.error({ err }, 'Failed to generate download URL');\n    throw new Error('Failed to generate download URL');\n  }\n}\n",
      "type": "registry:lib"
    },
    {
      "path": "registry/default/storage/s3-client.ts",
      "content": "import { S3Client } from '@aws-sdk/client-s3';\n\nlet s3Client: S3Client | null = null;\n\nexport const getS3Client = (): S3Client => {\n  if (s3Client) {\n    return s3Client;\n  }\n\n  if (\n    !process.env.S3_REGION ||\n    !process.env.S3_ENDPOINT ||\n    !process.env.S3_ACCESS_KEY ||\n    !process.env.S3_SECRET_KEY\n  ) {\n    throw new Error('Missing S3 environment variables');\n  }\n\n  s3Client = new S3Client({\n    region: process.env.S3_REGION,\n    endpoint: process.env.S3_ENDPOINT,\n    forcePathStyle: true, // Required for some S3-compatible services\n    credentials: {\n      accessKeyId: process.env.S3_ACCESS_KEY,\n      secretAccessKey: process.env.S3_SECRET_KEY,\n    },\n  });\n\n  return s3Client;\n};\n",
      "type": "registry:lib"
    }
  ],
  "docs": "Remember to configure CORS settings in your S3 bucket or compatible storage provider to allow downloads from your application's domain. This is required for the presigned URL downloads to work correctly. This function pairs nicely with the `download-file` item from this registry to generate a presigned download URL for a file."
}