Merge pull request #71 from docmost/aws_env_option

Add support for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
This commit is contained in:
Philip Okugbe
2024-07-07 09:49:31 +01:00
committed by GitHub
3 changed files with 22 additions and 9 deletions

View File

@ -34,9 +34,6 @@ export class S3Driver implements StorageDriver {
});
await this.s3Client.send(command);
// we can get the path from location
console.log(`File uploaded successfully: ${filePath}`);
} catch (err) {
throw new Error(`Failed to upload file: ${(err as Error).message}`);
}

View File

@ -41,20 +41,34 @@ export const storageDriverConfigProvider = {
};
case StorageOption.S3:
return {
const s3Config = {
driver,
config: {
region: environmentService.getAwsS3Region(),
endpoint: environmentService.getAwsS3Endpoint(),
bucket: environmentService.getAwsS3Bucket(),
baseUrl: environmentService.getAwsS3Url(),
credentials: {
accessKeyId: environmentService.getAwsS3AccessKeyId(),
secretAccessKey: environmentService.getAwsS3SecretAccessKey(),
},
credentials: undefined,
},
};
/**
* This makes use of AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY if present,
* If not present, it makes it lenient for the AWS SDK to use
* AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY if they are present in the environment
*/
if (
environmentService.getAwsS3AccessKeyId() ||
environmentService.getAwsS3SecretAccessKey()
) {
s3Config.config.credentials = {
accessKeyId: environmentService.getAwsS3AccessKeyId(),
secretAccessKey: environmentService.getAwsS3SecretAccessKey(),
};
}
return s3Config;
default:
throw new Error(`Unknown storage driver: ${driver}`);
}

View File

@ -1,15 +1,17 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { STORAGE_DRIVER_TOKEN } from './constants/storage.constants';
import { StorageDriver } from './interfaces';
@Injectable()
export class StorageService {
private readonly logger = new Logger(StorageService.name);
constructor(
@Inject(STORAGE_DRIVER_TOKEN) private storageDriver: StorageDriver,
) {}
async upload(filePath: string, fileContent: Buffer | any) {
await this.storageDriver.upload(filePath, fileContent);
this.logger.debug(`File uploaded successfully. Path: ${filePath}`);
}
async read(filePath: string): Promise<Buffer> {