fix(results): 🐛 Remove storage on result delete
This commit is contained in:
@ -1,11 +1,10 @@
|
||||
import { AWSError, config, Endpoint, S3 } from 'aws-sdk'
|
||||
import { PromiseResult } from 'aws-sdk/lib/request'
|
||||
import { Client } from 'minio'
|
||||
|
||||
export const deleteFiles = async ({
|
||||
urls,
|
||||
}: {
|
||||
urls: string[]
|
||||
}): Promise<PromiseResult<S3.DeleteObjectsOutput, AWSError>> => {
|
||||
}): Promise<void> => {
|
||||
if (
|
||||
!process.env.S3_ENDPOINT ||
|
||||
!process.env.S3_ACCESS_KEY ||
|
||||
@ -15,30 +14,23 @@ export const deleteFiles = async ({
|
||||
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
|
||||
)
|
||||
|
||||
const sslEnabled =
|
||||
const useSSL =
|
||||
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true
|
||||
config.update({
|
||||
accessKeyId: process.env.S3_ACCESS_KEY,
|
||||
secretAccessKey: process.env.S3_SECRET_KEY,
|
||||
const minioClient = new Client({
|
||||
endPoint: process.env.S3_ENDPOINT,
|
||||
port: process.env.S3_PORT ? parseInt(process.env.S3_PORT) : undefined,
|
||||
useSSL,
|
||||
accessKey: process.env.S3_ACCESS_KEY,
|
||||
secretKey: process.env.S3_SECRET_KEY,
|
||||
region: process.env.S3_REGION,
|
||||
sslEnabled,
|
||||
})
|
||||
const protocol = sslEnabled ? 'https' : 'http'
|
||||
const s3 = new S3({
|
||||
endpoint: new Endpoint(
|
||||
`${protocol}://${process.env.S3_ENDPOINT}${
|
||||
process.env.S3_PORT ? `:${process.env.S3_PORT}` : ''
|
||||
}`
|
||||
),
|
||||
})
|
||||
|
||||
const Bucket = process.env.S3_BUCKET ?? 'typebot'
|
||||
return s3
|
||||
.deleteObjects({
|
||||
Bucket,
|
||||
Delete: {
|
||||
Objects: urls.map((url) => ({ Key: url.split(`/${Bucket}/`)[1] })),
|
||||
},
|
||||
})
|
||||
.promise()
|
||||
const bucket = process.env.S3_BUCKET ?? 'typebot'
|
||||
|
||||
return minioClient.removeObjects(
|
||||
bucket,
|
||||
urls
|
||||
.filter((url) => url.includes(process.env.S3_ENDPOINT as string))
|
||||
.map((url) => url.split(`/${bucket}/`)[1])
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user