Skip to content

Commit 8606ed8

Browse files
[dev] [Marfuen] mariano/more-5 (#1585)
* chore(s3): update S3 bucket handling to use environment variable --------- Co-authored-by: Mariano Fuentes <marfuen98@gmail.com>
1 parent 27b8022 commit 8606ed8

8 files changed

Lines changed: 18 additions & 68 deletions

File tree

apps/app/src/ai/tools/store-to-s3.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
1+
import { s3Client } from '@/app/s3';
2+
import { PutObjectCommand } from '@aws-sdk/client-s3';
23
import type { UIMessage, UIMessageStreamWriter } from 'ai';
34
import { tool } from 'ai';
45
import z from 'zod/v3';
@@ -8,25 +9,14 @@ interface Params {
89
writer: UIMessageStreamWriter<UIMessage<never, DataPart>>;
910
}
1011

11-
const DEFAULTS = {
12-
bucket: 'comp-testing-lambda-tasks',
13-
region: 'us-east-1',
14-
orgId: 'org_689ce3dced87cc45f600a04b',
15-
taskId: 'tsk_689ce3dd6f19f4cf1f0ea061',
16-
};
17-
1812
const inputSchema = z.object({
1913
content: z.string().min(1).describe('The full file content to store'),
20-
bucket: z.string().optional().describe('Target S3 bucket'),
21-
region: z.string().optional().describe('AWS region for the S3 bucket'),
2214
orgId: z.string().optional().describe('Organization identifier'),
2315
taskId: z.string().optional().describe('Task identifier'),
2416
contentType: z.string().optional().describe('MIME type, defaults to text/plain for generic code'),
2517
});
2618
interface ToolInput {
2719
content: string;
28-
bucket?: string;
29-
region?: string;
3020
orgId?: string;
3121
taskId?: string;
3222
contentType?: string;
@@ -41,7 +31,7 @@ export const storeToS3 = ({ writer }: Params) => {
4131
const { toolCallId } = ctx;
4232
const parsed: unknown = inputSchema.parse(args);
4333
const input = parsed as ToolInput;
44-
const { content, bucket, region, orgId, taskId, contentType } = input;
34+
const { content, orgId, taskId, contentType } = input;
4535

4636
// Validate task format: must export a function via module.exports
4737
// Validate task format: must export a function with only event parameter
@@ -79,10 +69,9 @@ export const storeToS3 = ({ writer }: Params) => {
7969
});
8070
return message;
8171
}
82-
const resolvedBucket = bucket || DEFAULTS.bucket;
83-
const resolvedRegion = region || DEFAULTS.region;
84-
const resolvedOrgId = orgId || DEFAULTS.orgId;
85-
const resolvedTaskId = taskId || DEFAULTS.taskId;
72+
const resolvedBucket = process.env.TASKS_AUTOMATION_BUCKET;
73+
const resolvedOrgId = orgId;
74+
const resolvedTaskId = taskId;
8675
const keyBase = `${resolvedOrgId}/${resolvedTaskId}`;
8776
const key = `${keyBase}.automation.js`;
8877

@@ -93,20 +82,11 @@ export const storeToS3 = ({ writer }: Params) => {
9382
status: 'uploading',
9483
bucket: resolvedBucket,
9584
key,
96-
region: resolvedRegion,
9785
},
9886
});
9987

10088
try {
101-
const credentials =
102-
process.env.APP_AWS_ACCESS_KEY_ID && process.env.APP_AWS_SECRET_ACCESS_KEY
103-
? {
104-
accessKeyId: process.env.APP_AWS_ACCESS_KEY_ID as string,
105-
secretAccessKey: process.env.APP_AWS_SECRET_ACCESS_KEY as string,
106-
}
107-
: undefined;
108-
const s3 = new S3Client({ region: resolvedRegion, credentials });
109-
await s3.send(
89+
await s3Client.send(
11090
new PutObjectCommand({
11191
Bucket: resolvedBucket,
11292
Key: key,
@@ -130,7 +110,6 @@ export const storeToS3 = ({ writer }: Params) => {
130110
status: 'done',
131111
bucket: resolvedBucket,
132112
key,
133-
region: resolvedRegion,
134113
},
135114
});
136115

@@ -144,7 +123,6 @@ export const storeToS3 = ({ writer }: Params) => {
144123
status: 'error',
145124
bucket: resolvedBucket,
146125
key,
147-
region: resolvedRegion,
148126
error: { message },
149127
},
150128
});

apps/app/src/app/api/tasks-automations/chat/prompt.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ Most APIs require specific identifiers:
276276
- Reply with ONLY "✓ Created automation script and saved to S3"
277277
278278
2. S3 Storage Details:
279-
- Bucket: `comp-testing-lambda-tasks`
280-
- Region: `us-east-1`
281279
- Key: `{ORG_ID}/{TASK_ID}.automation.js` (from ACTUAL_VALUES_JSON)
282280
- The storeToS3 tool will handle all metadata automatically
283281

apps/app/src/app/api/tasks-automations/lambda/upload/route.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,15 @@ import { s3Client } from '@/app/s3';
22
import { PutObjectCommand } from '@aws-sdk/client-s3';
33
import { NextResponse } from 'next/server';
44

5-
const DEFAULTS = {
6-
bucket: 'comp-testing-lambda-tasks',
7-
region: 'us-east-1',
8-
};
9-
105
export async function POST(req: Request) {
116
try {
12-
const {
13-
orgId,
14-
taskId,
15-
content,
16-
bucket,
17-
region,
18-
}: { orgId: string; taskId: string; content: string; bucket?: string; region?: string } =
7+
const { orgId, taskId, content }: { orgId: string; taskId: string; content: string } =
198
await req.json();
209
if (!orgId || !taskId || typeof content !== 'string') {
2110
return NextResponse.json({ error: 'Missing orgId, taskId or content' }, { status: 400 });
2211
}
2312

24-
const resolvedBucket = bucket || DEFAULTS.bucket;
25-
const resolvedRegion = region || DEFAULTS.region;
13+
const resolvedBucket = process.env.TASKS_AUTOMATION_BUCKET;
2614
const key = `${orgId}/${taskId}.js`;
2715

2816
await s3Client.send(

apps/app/src/app/api/tasks-automations/s3/get/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function GET(req: Request) {
1616
// Get object from S3
1717
const { Body } = await s3Client.send(
1818
new GetObjectCommand({
19-
Bucket: process.env.TASKS_AUTOMATION_BUCKET || 'comp-testing-lambda-tasks',
19+
Bucket: process.env.TASKS_AUTOMATION_BUCKET,
2020
Key: key,
2121
}),
2222
);

apps/app/src/app/api/tasks-automations/s3/list/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function GET(req: Request) {
1616
// List objects in the organization's folder
1717
const response = await s3Client.send(
1818
new ListObjectsV2Command({
19-
Bucket: process.env.TASKS_AUTOMATION_BUCKET || 'comp-testing-lambda-tasks',
19+
Bucket: process.env.TASKS_AUTOMATION_BUCKET,
2020
Prefix: `${orgId}/`,
2121
MaxKeys: 100,
2222
}),

apps/app/src/app/api/tasks-automations/s3/upload/route.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ export async function POST(req: Request) {
1414

1515
// Determine the S3 key based on the type
1616
const s3Key = type === 'lambda' ? `${orgId}/${taskId}.js` : `${orgId}/${taskId}.${type}.js`;
17+
const bucket = process.env.TASKS_AUTOMATION_BUCKET;
1718

1819
// Upload to S3
1920
await s3Client.send(
2021
new PutObjectCommand({
21-
Bucket: process.env.TASKS_AUTOMATION_BUCKET || 'comp-testing-lambda-tasks',
22+
Bucket: bucket,
2223
Key: s3Key,
2324
Body: content,
2425
ContentType: 'application/javascript',
@@ -35,7 +36,7 @@ export async function POST(req: Request) {
3536

3637
return NextResponse.json({
3738
success: true,
38-
bucket: 'comp-testing-lambda-tasks',
39+
bucket: bucket,
3940
key: s3Key,
4041
message: 'Script uploaded successfully',
4142
});

apps/app/src/app/api/tasks-automations/trigger/execute/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function POST(req: Request) {
1717
try {
1818
const { Body } = await s3Client.send(
1919
new GetObjectCommand({
20-
Bucket: process.env.TASKS_AUTOMATION_BUCKET || 'comp-testing-lambda-tasks',
20+
Bucket: process.env.TASKS_AUTOMATION_BUCKET,
2121
Key: `${orgId}/${taskId}.automation.js`,
2222
}),
2323
);

apps/app/src/jobs/tasks/automation/execute-script.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getModelOptions } from '@/ai/gateway';
2+
import { s3Client } from '@/app/s3';
23
import { decrypt, type EncryptedData } from '@/lib/encryption';
3-
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
4+
import { GetObjectCommand } from '@aws-sdk/client-s3';
45
import { db } from '@db';
56
import { logger, queue, task } from '@trigger.dev/sdk';
67
import { generateObject } from 'ai';
@@ -37,32 +38,16 @@ export const executeAutomationScript = task({
3738
const { orgId, taskId } = payload;
3839
const logs: string[] = [];
3940

40-
if (
41-
!process.env.APP_AWS_REGION ||
42-
!process.env.APP_AWS_ACCESS_KEY_ID ||
43-
!process.env.APP_AWS_SECRET_ACCESS_KEY
44-
) {
45-
throw new Error('AWS S3 credentials or configuration missing. Check environment variables.');
46-
}
47-
4841
try {
4942
logger.info(`Executing automation script for task ${taskId} in org ${orgId}`);
5043

5144
// Fetch the script from S3
5245
const scriptKey = `${orgId}/${taskId}.automation.js`;
5346
logs.push(`[SYSTEM] Fetching script from S3: ${scriptKey}`);
5447

55-
const s3Client = new S3Client({
56-
region: process.env.APP_AWS_REGION,
57-
credentials: {
58-
accessKeyId: process.env.APP_AWS_ACCESS_KEY_ID,
59-
secretAccessKey: process.env.APP_AWS_SECRET_ACCESS_KEY,
60-
},
61-
});
62-
6348
const { Body } = await s3Client.send(
6449
new GetObjectCommand({
65-
Bucket: process.env.TASKS_AUTOMATION_BUCKET || 'comp-testing-lambda-tasks',
50+
Bucket: process.env.TASKS_AUTOMATION_BUCKET,
6651
Key: scriptKey,
6752
}),
6853
);

0 commit comments

Comments
 (0)