-
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.
#67 Convert s3 and sqs service into DI classes
- Loading branch information
1 parent
955b612
commit 6dce99e
Showing
7 changed files
with
135 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; | ||
import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner'; | ||
import { createRequest } from '@aws-sdk/util-create-request'; | ||
import { formatUrl } from '@aws-sdk/util-format-url'; | ||
|
||
import config from '../config/config'; | ||
|
||
export class S3FileService { | ||
#s3Client: S3Client; | ||
constructor() { | ||
this.#s3Client = new S3Client({ region: config.AWS_REGION }); | ||
} | ||
|
||
async generateSignedUploadUrl(key: string): Promise<string> { | ||
const request = await createRequest( | ||
this.#s3Client, | ||
new PutObjectCommand({ | ||
Key: key, | ||
Bucket: config.AWS_BUCKET_NAME, | ||
}), | ||
); | ||
|
||
const signer = new S3RequestPresigner({ | ||
...this.#s3Client.config, | ||
}); | ||
|
||
const url = await signer.presign(request, { | ||
expiresIn: 3600, | ||
}); | ||
return formatUrl(url); | ||
} | ||
|
||
keyToUrl(key: string): string { | ||
return `${config.FILE_ACCESS_BASE_URL}/${key}`; | ||
} | ||
|
||
createKey(resourceId: string, fileName: string) { | ||
return `${resourceId}/${fileName}`; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
import { SQSClient, ReceiveMessageCommand, Message, DeleteMessageCommand } from '@aws-sdk/client-sqs'; | ||
|
||
import config from '../config/config'; | ||
import { persistence } from '../persistence/persistence'; | ||
|
||
interface S3MessageContent { | ||
Records?: S3MessageContentRecord[]; | ||
} | ||
|
||
interface S3MessageContentRecord { | ||
eventTime: string; | ||
eventName: string; | ||
s3: { | ||
object: { | ||
key: string; | ||
size: number; | ||
}; | ||
}; | ||
} | ||
|
||
export class SQSQueueService { | ||
#client: SQSClient; | ||
|
||
constructor() { | ||
this.#client = new SQSClient({ region: config.AWS_REGION }); | ||
} | ||
|
||
async subscribeToFileUploads() { | ||
// todo exit this loop when app entering shutdown state. | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
console.log(`Polling ${config.AWS_FILE_UPLOADED_SQS_QUEUE_URL} for messages`); | ||
const result = await this.#client.send( | ||
new ReceiveMessageCommand({ | ||
QueueUrl: config.AWS_FILE_UPLOADED_SQS_QUEUE_URL, | ||
WaitTimeSeconds: 10, | ||
}), | ||
); | ||
if (result.Messages) { | ||
await Promise.all(result.Messages.map((message) => this.processMessage(message))); | ||
} | ||
} | ||
} | ||
|
||
async processMessage(message: Message) { | ||
if (message.Body) { | ||
const messageBody = JSON.parse(message.Body); | ||
if (messageBody.Message) { | ||
const messageData: S3MessageContent = JSON.parse(messageBody.Message); | ||
if (messageData.Records) { | ||
await Promise.all(messageData.Records.map((record) => this.processUploadedItem(record))); | ||
} | ||
} else { | ||
console.warn(`Unexpected empty inner message body`, message); | ||
} | ||
} else { | ||
console.warn(`Unexpected empty message body`, message); | ||
} | ||
|
||
await this.#client.send( | ||
new DeleteMessageCommand({ | ||
QueueUrl: config.AWS_FILE_UPLOADED_SQS_QUEUE_URL, | ||
ReceiptHandle: message.ReceiptHandle, | ||
}), | ||
); | ||
} | ||
|
||
async processUploadedItem(record: S3MessageContentRecord) { | ||
console.log('Processing uploaded item'); | ||
if (record.eventName !== 'ObjectCreated:Put') { | ||
console.warn(`Unexpected event name <${record.eventName}>`); | ||
} | ||
const key = record.s3.object.key; | ||
const quiz = await persistence.getQuizImage(key); | ||
if (quiz) { | ||
await persistence.markQuizImageReady(key); | ||
} else { | ||
console.error(`Invalid file upload at key: ${key}`); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { AuthenticationService } from './auth/authentication.service'; | ||
import { AuthorisationService } from './auth/authorisation.service'; | ||
import { S3FileService } from './file/s3.service'; | ||
import { SQSQueueService } from './queue/sqs.service'; | ||
|
||
// auth | ||
export const authenticationService = new AuthenticationService(); | ||
export const authorisationService = new AuthorisationService(); | ||
|
||
// file | ||
export const fileService = new S3FileService(); | ||
|
||
// queue | ||
export const queueService = new SQSQueueService(); |