4519 graphql change so we use delete multiple command when deleting files from s3 (#159)

* now use deleteObjects plural instead of single delete

* fixed when nothing is removed
This commit is contained in:
Arwid Thornström
2023-10-25 14:14:41 +02:00
committed by GitHub
parent b2a296c857
commit 2a9b7a88ee
2 changed files with 12 additions and 10 deletions
+6 -3
View File
@@ -4,6 +4,7 @@ import {
DeleteObjectCommand,
PutObjectCommand,
ListObjectsV2Command,
DeleteObjectsCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
export const EXPIRES_DEFAULT_SECS = 300;
@@ -82,13 +83,15 @@ export const listS3Files = async (
export const deleteFiles = async (
bucket: string,
key: string,
keys: string[],
): Promise<any> => {
try {
const s3Client = new S3Client({ region: 'eu-west-1' });
const deleteCmd = new DeleteObjectCommand({
const deleteCmd = new DeleteObjectsCommand({
Bucket: bucket,
Key: key,
Delete: {
Objects: keys.map((key) => ({ Key: key })),
},
});
return s3Client.send(deleteCmd);
} catch (err) {
+6 -7
View File
@@ -1044,15 +1044,14 @@ export class ProductAPI extends BaseSQLDataSource {
}
// Setup all delete actions
const deletePromises = shouldRemove.map((file) => {
return deleteFiles(CONFIG.imageBucket, file);
});
// Run all delete in parallell
await Promise.all(deletePromises);
let removed = [];
if (shouldRemove.length > 0) {
const response = await deleteFiles(CONFIG.imageBucket, shouldRemove);
removed = response.Deleted?.map((item) => item.Key) || [];
}
// Return what we did
return { active: shouldKeep, deleted: shouldRemove };
return { active: shouldKeep, deleted: removed };
}
/**