From 51a364b41d9a007e2ad97faa67a9ed83fce9ef0c Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Tue, 4 Mar 2025 04:05:43 +0100 Subject: [PATCH] Updated deprecated S3 media delete command. --- src/media.js | 58 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/media.js b/src/media.js index 2f03e66c..3e9a5e84 100755 --- a/src/media.js +++ b/src/media.js @@ -17,7 +17,7 @@ const { format } = require('date-fns'); const taskQueue = require('promise-task-queue'); const { Upload } = require('@aws-sdk/lib-storage'); -const { S3Client } = require('@aws-sdk/client-s3'); +const { S3Client, DeleteObjectsCommand } = require('@aws-sdk/client-s3'); const logger = require('./logger')(__filename); const argv = require('./argv'); @@ -1000,32 +1000,41 @@ async function associateAvatars(profiles) { } async function deleteS3Objects(allMedia) { - const statuses = await chunk(allMedia).reduce(async (chain, media) => { - const acc = await chain; + try { + const statuses = await chunk(allMedia).reduce(async (chain, media) => { + const acc = await chain; - const objects = media - .map((item) => [ - { Key: item.path }, - { Key: item.thumbnail }, - { Key: item.lazy }, - ]) - .flat() - .filter((item) => item.Key); + const objects = media + .map((item) => [ + { Key: item.path }, + { Key: item.thumbnail }, + { Key: item.lazy }, + ]) + .flat() + .filter((item) => item.Key); - const status = await s3.deleteObjects({ - Bucket: config.s3.bucket, - Delete: { - Objects: objects, - Quiet: false, - }, + const deleteCommand = new DeleteObjectsCommand({ + Bucket: config.s3.bucket, + Delete: { + Objects: objects, + Quiet: false, + }, + }); + + const status = await s3.send(deleteCommand); + + logger.info(`Removed ${status.Deleted.length} media files from S3 bucket '${config.s3.bucket}', ${status.Errors.length} errors`); + + return acc.concat(status); + }, Promise.resolve()).catch((error) => { + console.log(error); }); - logger.info(`Removed ${status.Deleted.length} media files from S3 bucket '${config.s3.bucket}', ${status.Errors.length} errors`); - - return acc.concat(status); - }); - - return statuses; + return statuses; + } catch (error) { + console.log(error); + throw error; + } } async function flushOrphanedMedia(stage = 1) { @@ -1060,7 +1069,8 @@ async function flushOrphanedMedia(stage = 1) { ) .whereRaw('associations.media_id = media.id'), ) - .limit(config.media.flushWindow); + // .limit(config.media.flushWindow); + .limit(1); // .delete(); logger.info(`Found ${orphanedMedia.length} orphaned media entries in stage ${stage}`);