From 668274ce381bb5ec98b8dc3e5831199894ab05fa Mon Sep 17 00:00:00 2001 From: tshuli Date: Wed, 13 Sep 2023 17:07:00 +0800 Subject: [PATCH] chore: enable versioning for clean bucket --- init-localstack.sh | 2 +- serverless/virus-scanner/src/index.ts | 7 ++++++- serverless/virus-scanner/src/s3.service.ts | 22 ++++++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/init-localstack.sh b/init-localstack.sh index ee8569ddd8..86cb788e05 100755 --- a/init-localstack.sh +++ b/init-localstack.sh @@ -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 diff --git a/serverless/virus-scanner/src/index.ts b/serverless/virus-scanner/src/index.ts index d4ba752328..a31fc37280 100644 --- a/serverless/virus-scanner/src/index.ts +++ b/serverless/virus-scanner/src/index.ts @@ -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, @@ -174,6 +177,7 @@ export const handler = async ( logger.info({ message: 'clean file moved to clean bucket', cleanFileKey, + destinationVersionId, }) return { @@ -181,6 +185,7 @@ export const handler = async ( body: JSON.stringify({ message: 'File scan completed', cleanFileKey, + destinationVersionId, }), } } diff --git a/serverless/virus-scanner/src/s3.service.ts b/serverless/virus-scanner/src/s3.service.ts index ab01dfa6f8..1dd6c78f0f 100644 --- a/serverless/virus-scanner/src/s3.service.ts +++ b/serverless/virus-scanner/src/s3.service.ts @@ -132,7 +132,7 @@ export class S3Service { sourceObjectVersionId, destinationBucketName, destinationObjectKey, - }: MoveS3FileParams) { + }: MoveS3FileParams): Promise { this.logger.info( { sourceBucketName, @@ -145,7 +145,7 @@ export class S3Service { ) try { - await this.s3Client.send( + const { VersionId } = await this.s3Client.send( new CopyObjectCommand({ Key: destinationObjectKey, Bucket: destinationBucketName, @@ -153,6 +153,21 @@ export class S3Service { }), ) + 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, @@ -168,9 +183,12 @@ export class S3Service { sourceObjectVersionId, destinationBucketName, destinationObjectKey, + destinationVersionId: VersionId, }, 'Moved document in s3', ) + + return VersionId } catch (error) { this.logger.error( {