-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from BinaryStudioAcademy/task/OV-37-integrate-…
…aws-s3-and-file-uploading-plugin OV-37: Integrate AWS S3 and add file upload plugin
- Loading branch information
Showing
8 changed files
with
1,896 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
GetObjectCommand, | ||
PutObjectCommand, | ||
S3Client, | ||
} from '@aws-sdk/client-s3'; | ||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; | ||
|
||
import { type BaseConfig } from '~/common/config/base-config.package.js'; | ||
|
||
class FileService { | ||
private config: BaseConfig; | ||
private client: S3Client; | ||
private bucketName: string; | ||
private cfDistributionId: string; | ||
|
||
public constructor(config: BaseConfig) { | ||
this.config = config; | ||
|
||
this.client = this.initClient(); | ||
this.bucketName = this.config.ENV.AWS.S3.BUCKET_NAME; | ||
this.cfDistributionId = this.config.ENV.AWS.CLOUDFRONT.DOMAIN_ID; | ||
} | ||
|
||
private initClient(): S3Client { | ||
return new S3Client({ | ||
credentials: { | ||
accessKeyId: this.config.ENV.AWS.ACCESS_KEY_ID, | ||
secretAccessKey: this.config.ENV.AWS.SECRET_ACCESS_KEY, | ||
}, | ||
region: this.config.ENV.AWS.S3.REGION, | ||
}); | ||
} | ||
|
||
public uploadFile = async ( | ||
buffer: Buffer, | ||
fileName: string, | ||
): Promise<void> => { | ||
const command = new PutObjectCommand({ | ||
Bucket: this.bucketName, | ||
Key: fileName, | ||
Body: buffer, | ||
}); | ||
|
||
await this.client.send(command); | ||
}; | ||
|
||
public getFileUrl = async (fileName: string): Promise<string> => { | ||
const getFileObject = new GetObjectCommand({ | ||
Bucket: this.bucketName, | ||
Key: fileName, | ||
}); | ||
|
||
return await getSignedUrl(this.client, getFileObject, { | ||
expiresIn: 3600, | ||
}); | ||
}; | ||
|
||
public getCloudFrontFileUrl = (fileName: string): string => { | ||
return `https://${this.cfDistributionId}.cloudfront.net/${fileName}`; | ||
}; | ||
} | ||
|
||
export { FileService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { config } from '../config/config.js'; | ||
import { CryptService } from './crypt/crypt.service.js'; | ||
import { FileService } from './file/file.service.js'; | ||
|
||
const cryptService = new CryptService(); | ||
const fileService = new FileService(config); | ||
|
||
export { cryptService }; | ||
export { cryptService, fileService }; |
Oops, something went wrong.