|
1 | 1 | import { logger, task } from '@trigger.dev/sdk'; |
2 | | -import { APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET, BUCKET_NAME, extractS3KeyFromUrl, s3Client } from '@/app/s3'; |
| 2 | +import { extractS3KeyFromUrl } from '@/app/s3'; |
3 | 3 | import { env } from '@/env.mjs'; |
4 | | -import { GetObjectCommand } from '@aws-sdk/client-s3'; |
| 4 | +import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; |
5 | 5 | import { openai } from '@ai-sdk/openai'; |
6 | 6 | import { db } from '@db'; |
7 | 7 | import { generateObject, generateText, jsonSchema } from 'ai'; |
@@ -272,9 +272,15 @@ async function extractContentFromAttachment( |
272 | 272 | throw new Error('Attachment not found'); |
273 | 273 | } |
274 | 274 |
|
| 275 | + const bucketName = process.env.APP_AWS_BUCKET_NAME; |
| 276 | + if (!bucketName) { |
| 277 | + throw new Error('APP_AWS_BUCKET_NAME environment variable is not set in Trigger.dev.'); |
| 278 | + } |
| 279 | + |
275 | 280 | const key = extractS3KeyFromUrl(attachment.url); |
| 281 | + const s3Client = createS3Client(); |
276 | 282 | const getCommand = new GetObjectCommand({ |
277 | | - Bucket: BUCKET_NAME!, |
| 283 | + Bucket: bucketName, |
278 | 284 | Key: key, |
279 | 285 | }); |
280 | 286 |
|
@@ -302,23 +308,47 @@ async function extractContentFromAttachment( |
302 | 308 | return { content, fileType }; |
303 | 309 | } |
304 | 310 |
|
| 311 | +/** |
| 312 | + * Creates an S3 client instance for Trigger.dev tasks |
| 313 | + * Reads environment variables directly (not from shared s3.ts module) |
| 314 | + */ |
| 315 | +function createS3Client(): S3Client { |
| 316 | + const region = process.env.APP_AWS_REGION || 'us-east-1'; |
| 317 | + const accessKeyId = process.env.APP_AWS_ACCESS_KEY_ID; |
| 318 | + const secretAccessKey = process.env.APP_AWS_SECRET_ACCESS_KEY; |
| 319 | + |
| 320 | + if (!accessKeyId || !secretAccessKey) { |
| 321 | + throw new Error( |
| 322 | + 'AWS S3 credentials are missing. Please set APP_AWS_ACCESS_KEY_ID and APP_AWS_SECRET_ACCESS_KEY environment variables in Trigger.dev.', |
| 323 | + ); |
| 324 | + } |
| 325 | + |
| 326 | + return new S3Client({ |
| 327 | + region, |
| 328 | + credentials: { |
| 329 | + accessKeyId, |
| 330 | + secretAccessKey, |
| 331 | + }, |
| 332 | + }); |
| 333 | +} |
| 334 | + |
305 | 335 | /** |
306 | 336 | * Extracts content from an S3 key (for temporary questionnaire files) |
307 | 337 | */ |
308 | 338 | async function extractContentFromS3Key( |
309 | 339 | s3Key: string, |
310 | 340 | fileType: string, |
311 | 341 | ): Promise<{ content: string; fileType: string }> { |
312 | | - if (!s3Client) { |
313 | | - throw new Error('S3 client is not initialized. Please check AWS S3 environment variables (APP_AWS_REGION, APP_AWS_ACCESS_KEY_ID, APP_AWS_SECRET_ACCESS_KEY).'); |
| 342 | + const questionnaireBucket = process.env.APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET; |
| 343 | + |
| 344 | + if (!questionnaireBucket) { |
| 345 | + throw new Error('Questionnaire upload bucket is not configured. Please set APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET environment variable in Trigger.dev.'); |
314 | 346 | } |
315 | 347 |
|
316 | | - if (!APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET) { |
317 | | - throw new Error('Questionnaire upload bucket is not configured. Please set APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET environment variable.'); |
318 | | - } |
| 348 | + const s3Client = createS3Client(); |
319 | 349 |
|
320 | 350 | const getCommand = new GetObjectCommand({ |
321 | | - Bucket: APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET, |
| 351 | + Bucket: questionnaireBucket, |
322 | 352 | Key: s3Key, |
323 | 353 | }); |
324 | 354 |
|
|
0 commit comments