Skip to content

Commit 4079b73

Browse files
feat(questionnaire): enhance S3 client creation on parse action (#1760)
Co-authored-by: Tofik Hasanov <annexcies@gmail.com>
1 parent 1ba8866 commit 4079b73

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

apps/app/src/jobs/tasks/vendors/parse-questionnaire.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
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';
33
import { env } from '@/env.mjs';
4-
import { GetObjectCommand } from '@aws-sdk/client-s3';
4+
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
55
import { openai } from '@ai-sdk/openai';
66
import { db } from '@db';
77
import { generateObject, generateText, jsonSchema } from 'ai';
@@ -272,9 +272,15 @@ async function extractContentFromAttachment(
272272
throw new Error('Attachment not found');
273273
}
274274

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+
275280
const key = extractS3KeyFromUrl(attachment.url);
281+
const s3Client = createS3Client();
276282
const getCommand = new GetObjectCommand({
277-
Bucket: BUCKET_NAME!,
283+
Bucket: bucketName,
278284
Key: key,
279285
});
280286

@@ -302,23 +308,47 @@ async function extractContentFromAttachment(
302308
return { content, fileType };
303309
}
304310

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+
305335
/**
306336
* Extracts content from an S3 key (for temporary questionnaire files)
307337
*/
308338
async function extractContentFromS3Key(
309339
s3Key: string,
310340
fileType: string,
311341
): 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.');
314346
}
315347

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();
319349

320350
const getCommand = new GetObjectCommand({
321-
Bucket: APP_AWS_QUESTIONNAIRE_UPLOAD_BUCKET,
351+
Bucket: questionnaireBucket,
322352
Key: s3Key,
323353
});
324354

0 commit comments

Comments
 (0)