diff --git a/backend/src/common/config/base-config.package.ts b/backend/src/common/config/base-config.package.ts index 95d949217..3fdeacd6e 100644 --- a/backend/src/common/config/base-config.package.ts +++ b/backend/src/common/config/base-config.package.ts @@ -68,30 +68,40 @@ class BaseConfig implements Config { default: null, }, }, - AWS_S3: { + AWS: { ACCESS_KEY_ID: { - doc: 'AWS S3 access key id', + doc: 'AWS access key id', format: String, - env: 'AWS_S3_ACCESS_KEY_ID', + env: 'AWS_ACCESS_KEY_ID', default: null, }, SECRET_ACCESS_KEY: { - doc: 'AWS S3 access key id', + doc: 'AWS secret access key', format: String, - env: 'AWS_S3_SECRET_ACCESS_KEY', + env: 'AWS_SECRET_ACCESS_KEY', default: null, }, - REGION: { - doc: 'AWS S3 region', - format: String, - env: 'AWS_S3_REGION', - default: null, + S3: { + REGION: { + doc: 'AWS S3 region', + format: String, + env: 'AWS_S3_REGION', + default: null, + }, + BUCKET_NAME: { + doc: 'AWS S3 bucket name', + format: String, + env: 'AWS_S3_BUCKET_NAME', + default: null, + }, }, - BUCKET_NAME: { - doc: 'AWS S3 bucket name', - format: String, - env: 'AWS_S3_BUCKET_NAME', - default: null, + CLOUDFRONT: { + DISTRIBUTION_ID: { + doc: 'AWS CloudFront distribution id', + format: String, + env: 'AWS_CLOUDFRONT_DISTRIBUTION_ID', + default: null, + }, }, }, }); diff --git a/backend/src/common/config/types/environment-schema.type.ts b/backend/src/common/config/types/environment-schema.type.ts index 415d65eee..740f34e26 100644 --- a/backend/src/common/config/types/environment-schema.type.ts +++ b/backend/src/common/config/types/environment-schema.type.ts @@ -12,11 +12,16 @@ type EnvironmentSchema = { POOL_MIN: number; POOL_MAX: number; }; - AWS_S3: { + AWS: { ACCESS_KEY_ID: string; SECRET_ACCESS_KEY: string; - REGION: string; - BUCKET_NAME: string; + S3: { + REGION: string; + BUCKET_NAME: string; + }; + CLOUDFRONT: { + DISTRIBUTION_ID: string; + }; }; }; diff --git a/backend/src/common/services/file/file.service.ts b/backend/src/common/services/file/file.service.ts index 112d7b5ae..cc020045f 100644 --- a/backend/src/common/services/file/file.service.ts +++ b/backend/src/common/services/file/file.service.ts @@ -11,19 +11,21 @@ class FileService { private config: BaseConfig; private client: S3Client; private bucketName: string; + private cfDistributionId: string; public constructor(config: BaseConfig) { this.config = config; this.client = new S3Client({ credentials: { - accessKeyId: this.config.ENV.AWS_S3.ACCESS_KEY_ID, - secretAccessKey: this.config.ENV.AWS_S3.SECRET_ACCESS_KEY, + accessKeyId: this.config.ENV.AWS.ACCESS_KEY_ID, + secretAccessKey: this.config.ENV.AWS.SECRET_ACCESS_KEY, }, - region: this.config.ENV.AWS_S3.REGION, + region: this.config.ENV.AWS.S3.REGION, }); - this.bucketName = this.config.ENV.AWS_S3.BUCKET_NAME; + this.bucketName = this.config.ENV.AWS.S3.BUCKET_NAME; + this.cfDistributionId = this.config.ENV.AWS.CLOUDFRONT.DISTRIBUTION_ID; } public uploadFile = async ( @@ -49,6 +51,10 @@ class FileService { expiresIn: 3600, }); }; + + public getCloudFrontFileUrl = (fileName: string): string => { + return `https://${this.cfDistributionId}.cloudfront.net/${fileName}`; + }; } export { FileService };