-
Notifications
You must be signed in to change notification settings - Fork 12
Enable AWS S3 image storage : Issue #208 #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
73b7b81
Enable AWS S3 image storage.
suratdas df7916b
Incorporated review comments.
suratdas 080c07c
Updated the tests.
suratdas 7a33d12
Merge branch 'master' into s3-integration
suratdas d2f18d7
Implemented AWS S3 image service.
suratdas ea4ea5e
Moved logic to static service
suratdas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 +1,66 @@ | ||
| import { PNGWithMetadata } from 'pngjs'; | ||
| import { PNG, PNGWithMetadata } from 'pngjs'; | ||
| import { Logger } from '@nestjs/common'; | ||
| import { Static } from '../static.interface'; | ||
| // import { S3Client } from '@aws-sdk/client-s3'; | ||
| import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
| import { Readable } from 'stream'; | ||
|
|
||
| export class AWSS3Service implements Static { | ||
| private readonly logger: Logger = new Logger(AWSS3Service.name); | ||
| // private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; | ||
| // private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; | ||
| // private readonly AWS_REGION = process.env.AWS_REGION; | ||
| // private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME; | ||
| private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; | ||
| private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; | ||
| private readonly AWS_REGION = process.env.AWS_REGION; | ||
| private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME; | ||
|
|
||
| // private s3Client: S3Client; | ||
| private s3Client: S3Client; | ||
|
|
||
| constructor() { | ||
| // this.s3Client = new S3Client({ | ||
| // credentials: { | ||
| // accessKeyId: this.AWS_ACCESS_KEY_ID, | ||
| // secretAccessKey: this.AWS_SECRET_ACCESS_KEY, | ||
| // }, | ||
| // region: this.AWS_REGION, | ||
| // }); | ||
| this.s3Client = new S3Client({ | ||
| credentials: { | ||
| accessKeyId: this.AWS_ACCESS_KEY_ID, | ||
| secretAccessKey: this.AWS_SECRET_ACCESS_KEY, | ||
| }, | ||
| region: this.AWS_REGION, | ||
| }); | ||
| this.logger.log('AWS S3 service is being used for file storage.'); | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> { | ||
| throw new Error('Method not implemented.'); | ||
|
|
||
| async saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> { | ||
| const imageName = `${Date.now()}.${type}.png`; | ||
| try { | ||
| await this.s3Client.send( | ||
| new PutObjectCommand({ | ||
| Bucket: this.AWS_S3_BUCKET_NAME, | ||
| Key: imageName, | ||
| ContentType: 'image/png', | ||
| Body: imageBuffer, | ||
| }) | ||
| ); | ||
| return imageName; | ||
| } catch (ex) { | ||
| throw new Error('Could not save file at AWS S3 : ' + ex); | ||
| } | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| getImage(fileName: string): Promise<PNGWithMetadata> { | ||
| throw new Error('Method not implemented.'); | ||
| async getImage(fileName: string): Promise<PNGWithMetadata> { | ||
| if (!fileName) return null; | ||
| try { | ||
| const command = new GetObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: fileName }); | ||
| const s3Response = await this.s3Client.send(command); | ||
| const stream = s3Response.Body as Readable; | ||
| return PNG.sync.read(Buffer.concat(await stream.toArray())); | ||
| } catch (ex) { | ||
| this.logger.error(`Error from read : Cannot get image: ${fileName}. ${ex}`); | ||
| } | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| deleteImage(imageName: string): Promise<boolean> { | ||
| throw new Error('Method not implemented.'); | ||
| async deleteImage(imageName: string): Promise<boolean> { | ||
| if (!imageName) return false; | ||
| try { | ||
| await this.s3Client.send(new DeleteObjectCommand({ Bucket: this.AWS_S3_BUCKET_NAME, Key: imageName })); | ||
| return true; | ||
| } catch (error) { | ||
| this.logger.log(`Failed to delete file at AWS S3 for image ${imageName}:`, error); | ||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,44 @@ | ||
| import { Controller, Get, Logger, Param, Res } from '@nestjs/common'; | ||
| import { Response } from 'express'; | ||
| import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
| import { isHddStaticServiceConfigured, isS3ServiceConfigured } from './utils'; | ||
| import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
| import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; | ||
|
|
||
| @ApiTags('images') | ||
| @Controller('images') | ||
| export class StaticController { | ||
| private readonly logger: Logger = new Logger(StaticController.name); | ||
|
|
||
| @Get('/:fileName') | ||
| @ApiOkResponse() | ||
| async getUrlAndRedirect(@Param('fileName') fileName: string, @Res() res: Response) { | ||
| try { | ||
| if (isHddStaticServiceConfigured()) { | ||
| res.redirect('/' + fileName); | ||
| } | ||
| if (isS3ServiceConfigured()) { | ||
| const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID; | ||
| const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY; | ||
| const AWS_REGION = process.env.AWS_REGION; | ||
| const AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME; | ||
|
|
||
| const s3Client = new S3Client({ | ||
| credentials: { | ||
| accessKeyId: `${AWS_ACCESS_KEY_ID}`, | ||
| secretAccessKey: `${AWS_SECRET_ACCESS_KEY}`, | ||
| }, | ||
| region: `${AWS_REGION}`, | ||
| }); | ||
| const command = new GetObjectCommand({ | ||
| Bucket: `${AWS_S3_BUCKET_NAME}`, | ||
| Key: fileName, | ||
| }); | ||
| res.redirect(await getSignedUrl(s3Client, command, { expiresIn: 3600 })); | ||
| } | ||
| } catch (error) { | ||
| this.logger.error('Error fetching file from S3:' + fileName, error); | ||
| res.status(500).send('Error occurred while getting the file.'); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,3 +1,7 @@ | ||
| export function isHddStaticServiceConfigured() { | ||
| return !process.env.STATIC_SERVICE || process.env.STATIC_SERVICE === 'hdd'; | ||
| } | ||
|
|
||
| export function isS3ServiceConfigured() { | ||
| return !process.env.STATIC_SERVICE || process.env.STATIC_SERVICE === 's3'; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move this logic to service layer like
getImageUrlfor both hdd and s3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Done.