Skip to content

Commit da39550

Browse files
authored
Merge pull request #1194 from trycompai/claudio/fix-file-upload
[dev] [claudfuen] claudio/fix-file-upload
2 parents 0ea5ac8 + 43d1232 commit da39550

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

apps/app/src/app/s3.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,45 @@ const APP_AWS_SECRET_ACCESS_KEY = process.env.APP_AWS_SECRET_ACCESS_KEY;
66

77
export const BUCKET_NAME = process.env.APP_AWS_BUCKET_NAME;
88

9-
if (!APP_AWS_ACCESS_KEY_ID || !APP_AWS_SECRET_ACCESS_KEY || !BUCKET_NAME || !APP_AWS_REGION) {
10-
// Log the error in production environments
11-
if (process.env.NODE_ENV === 'production') {
12-
console.error('AWS S3 credentials or configuration missing in environment variables.');
13-
} else {
14-
// Throw in development for immediate feedback
15-
throw new Error('AWS S3 credentials or configuration missing. Check environment variables.');
16-
}
17-
// Optionally, you could export a dummy/error client or null here
18-
// depending on how you want consuming code to handle the missing config.
19-
}
9+
let s3ClientInstance: S3Client;
2010

21-
// Create a single S3 client instance
22-
// Add null checks or assertions if the checks above don't guarantee non-null values
11+
const redact = (value?: string) => {
12+
if (!value) return 'undefined';
13+
return `${value.substring(0, 4)}****`;
14+
};
2315

24-
export const s3Client = new S3Client({
25-
region: APP_AWS_REGION!,
26-
credentials: {
27-
accessKeyId: APP_AWS_ACCESS_KEY_ID!,
28-
secretAccessKey: APP_AWS_SECRET_ACCESS_KEY!,
29-
},
30-
});
16+
try {
17+
if (!APP_AWS_ACCESS_KEY_ID || !APP_AWS_SECRET_ACCESS_KEY || !BUCKET_NAME || !APP_AWS_REGION) {
18+
throw new Error('AWS S3 credentials or configuration missing. Check environment variables.');
19+
}
3120

32-
// Ensure BUCKET_NAME is exported and non-null checked if needed elsewhere explicitly
33-
if (!BUCKET_NAME && process.env.NODE_ENV === 'production') {
34-
console.error('AWS_BUCKET_NAME is not defined.');
21+
s3ClientInstance = new S3Client({
22+
region: APP_AWS_REGION,
23+
credentials: {
24+
accessKeyId: APP_AWS_ACCESS_KEY_ID,
25+
secretAccessKey: APP_AWS_SECRET_ACCESS_KEY,
26+
},
27+
});
28+
} catch (error) {
29+
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
30+
console.error('!!! FAILED TO INITIALIZE S3 CLIENT !!!');
31+
console.error('!!! This is likely due to missing or invalid environment variables. !!!');
32+
console.error('--- Provided Configuration ---');
33+
console.error(`APP_AWS_REGION: ${APP_AWS_REGION}`);
34+
console.error(`APP_AWS_ACCESS_KEY_ID: ${redact(APP_AWS_ACCESS_KEY_ID)}`);
35+
console.error(`APP_AWS_SECRET_ACCESS_KEY: ${redact(APP_AWS_SECRET_ACCESS_KEY)}`);
36+
console.error(`APP_AWS_BUCKET_NAME: ${BUCKET_NAME}`);
37+
console.error('-----------------------------');
38+
console.error('Error:', error);
39+
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
40+
// Prevent the app from continuing without a valid S3 client
41+
// In a real-world scenario, you might have a fallback or a dummy client.
42+
// For now, we re-throw to make the crash explicit.
43+
throw error;
3544
}
3645

46+
export const s3Client = s3ClientInstance;
47+
3748
/**
3849
* Validates if a hostname is a valid AWS S3 endpoint
3950
*/

0 commit comments

Comments
 (0)