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:
+6
-3
@@ -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) {
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user