mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 10:20:15 +01:00
🚀 feat: S3 Integration for File handling and Image uploads (#6142)
* French Translation Update * French Translation Update * test * Add fileStrategy S3 Config * update s3 crud.js * 🔧 chore: downgrade dotenv to version 16.0.3 and add aws-sdk to package-lock.json * 🔧 chore: remove aws-sdk from package.json * 🚀 feat: Integrate AWS SDK for S3 with enhanced upload and retrieval functionalities * 🚀 feat: Implement S3 integration for file upload and retrieval functionalities * 🚀 feat: Enhance S3 initialization to support default credentials and improved error handling --------- Co-authored-by: Gael Martins <gael.martins@acolad.com>
This commit is contained in:
parent
19446cb864
commit
8f68e8be81
10 changed files with 3565 additions and 0 deletions
43
api/server/services/Files/S3/initialize.js
Normal file
43
api/server/services/Files/S3/initialize.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const { S3Client } = require('@aws-sdk/client-s3');
|
||||
const { logger } = require('~/config');
|
||||
|
||||
let s3 = null;
|
||||
|
||||
/**
|
||||
* Initializes and returns an instance of the AWS S3 client.
|
||||
*
|
||||
* If AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are provided, they will be used.
|
||||
* Otherwise, the AWS SDK's default credentials chain (including IRSA) is used.
|
||||
*
|
||||
* @returns {S3Client|null} An instance of S3Client if the region is provided; otherwise, null.
|
||||
*/
|
||||
const initializeS3 = () => {
|
||||
if (s3) {
|
||||
return s3;
|
||||
}
|
||||
|
||||
const region = process.env.AWS_REGION;
|
||||
if (!region) {
|
||||
logger.error('[initializeS3] AWS_REGION is not set. Cannot initialize S3.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
|
||||
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
|
||||
|
||||
if (accessKeyId && secretAccessKey) {
|
||||
s3 = new S3Client({
|
||||
region,
|
||||
credentials: { accessKeyId, secretAccessKey },
|
||||
});
|
||||
logger.info('[initializeS3] S3 initialized with provided credentials.');
|
||||
} else {
|
||||
// When using IRSA, credentials are automatically provided via the IAM Role attached to the ServiceAccount.
|
||||
s3 = new S3Client({ region });
|
||||
logger.info('[initializeS3] S3 initialized using default credentials (IRSA).');
|
||||
}
|
||||
|
||||
return s3;
|
||||
};
|
||||
|
||||
module.exports = { initializeS3 };
|
||||
Loading…
Add table
Add a link
Reference in a new issue