Updated deprecated S3 media delete command.

This commit is contained in:
DebaucheryLibrarian 2025-03-04 04:05:43 +01:00
parent 6fcd31f035
commit 51a364b41d
1 changed files with 34 additions and 24 deletions

View File

@ -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}`);