Skip to content

Commit

Permalink
chore: enable versioning for clean bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
tshuli committed Sep 13, 2023
1 parent a0904d2 commit 668274c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion init-localstack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ awslocal s3 mb s3://$STATIC_ASSETS_S3_BUCKET
awslocal s3 mb s3://$VIRUS_SCANNER_QUARANTINE_S3_BUCKET
awslocal s3api put-bucket-versioning --bucket $VIRUS_SCANNER_QUARANTINE_S3_BUCKET --versioning-configuration Status=Enabled
awslocal s3 mb s3://$VIRUS_SCANNER_CLEAN_S3_BUCKET

awslocal s3api put-bucket-versioning --bucket $VIRUS_SCANNER_CLEAN_S3_BUCKET --versioning-configuration Status=Enabled

set +x
7 changes: 6 additions & 1 deletion serverless/virus-scanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ export const handler = async (
key: quarantineFileKey,
versionId,
})

let destinationVersionId: string

try {
await s3Client.moveS3File({
destinationVersionId = await s3Client.moveS3File({
sourceBucketName: quarantineBucket,
sourceObjectKey: quarantineFileKey,
sourceObjectVersionId: versionId,
Expand All @@ -174,13 +177,15 @@ export const handler = async (
logger.info({
message: 'clean file moved to clean bucket',
cleanFileKey,
destinationVersionId,
})

return {
statusCode: StatusCodes.OK,
body: JSON.stringify({
message: 'File scan completed',
cleanFileKey,
destinationVersionId,
}),
}
}
Expand Down
22 changes: 20 additions & 2 deletions serverless/virus-scanner/src/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class S3Service {
sourceObjectVersionId,
destinationBucketName,
destinationObjectKey,
}: MoveS3FileParams) {
}: MoveS3FileParams): Promise<string> {
this.logger.info(
{
sourceBucketName,
Expand All @@ -145,14 +145,29 @@ export class S3Service {
)

try {
await this.s3Client.send(
const { VersionId } = await this.s3Client.send(
new CopyObjectCommand({
Key: destinationObjectKey,
Bucket: destinationBucketName,
CopySource: `${sourceBucketName}/${sourceObjectKey}?versionId=${sourceObjectVersionId}`,
}),
)

if (!VersionId) {
this.logger.error(
{
sourceBucketName,
sourceObjectKey,
sourceObjectVersionId,
destinationBucketName,
destinationObjectKey,
},
'VersionId is empty after copying object in s3',
)

throw new Error('VersionId is empty')
}

await this.s3Client.send(
new DeleteObjectCommand({
Key: sourceObjectKey,
Expand All @@ -168,9 +183,12 @@ export class S3Service {
sourceObjectVersionId,
destinationBucketName,
destinationObjectKey,
destinationVersionId: VersionId,
},
'Moved document in s3',
)

return VersionId
} catch (error) {
this.logger.error(
{
Expand Down

0 comments on commit 668274c

Please sign in to comment.