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,
|
DeleteObjectCommand,
|
||||||
PutObjectCommand,
|
PutObjectCommand,
|
||||||
ListObjectsV2Command,
|
ListObjectsV2Command,
|
||||||
|
DeleteObjectsCommand,
|
||||||
} from '@aws-sdk/client-s3';
|
} from '@aws-sdk/client-s3';
|
||||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||||
export const EXPIRES_DEFAULT_SECS = 300;
|
export const EXPIRES_DEFAULT_SECS = 300;
|
||||||
@@ -82,13 +83,15 @@ export const listS3Files = async (
|
|||||||
|
|
||||||
export const deleteFiles = async (
|
export const deleteFiles = async (
|
||||||
bucket: string,
|
bucket: string,
|
||||||
key: string,
|
keys: string[],
|
||||||
): Promise<any> => {
|
): Promise<any> => {
|
||||||
try {
|
try {
|
||||||
const s3Client = new S3Client({ region: 'eu-west-1' });
|
const s3Client = new S3Client({ region: 'eu-west-1' });
|
||||||
const deleteCmd = new DeleteObjectCommand({
|
const deleteCmd = new DeleteObjectsCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
Key: key,
|
Delete: {
|
||||||
|
Objects: keys.map((key) => ({ Key: key })),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
return s3Client.send(deleteCmd);
|
return s3Client.send(deleteCmd);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -1044,15 +1044,14 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup all delete actions
|
// Setup all delete actions
|
||||||
const deletePromises = shouldRemove.map((file) => {
|
let removed = [];
|
||||||
return deleteFiles(CONFIG.imageBucket, file);
|
if (shouldRemove.length > 0) {
|
||||||
});
|
const response = await deleteFiles(CONFIG.imageBucket, shouldRemove);
|
||||||
|
removed = response.Deleted?.map((item) => item.Key) || [];
|
||||||
// Run all delete in parallell
|
}
|
||||||
await Promise.all(deletePromises);
|
|
||||||
|
|
||||||
// Return what we did
|
// Return what we did
|
||||||
return { active: shouldKeep, deleted: shouldRemove };
|
return { active: shouldKeep, deleted: removed };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user