2
0

🧑‍💻 (s3) Correctly delete the files when deleting resources

This commit is contained in:
Baptiste Arnaud
2024-09-02 11:23:01 +02:00
parent f53705268c
commit 041b817aa0
16 changed files with 225 additions and 206 deletions

View File

@ -1,43 +0,0 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
export const deleteFilesFromBucket = async ({
urls,
}: {
urls: string[]
}): Promise<void> => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const bucket = env.S3_BUCKET
const keys = urls.reduce<string[]>(
(keys, url) => [
...keys,
...addKeyIfIncludesPublicCustomDomain(url),
...addKeyIfIncludesDefaultEndpoint(url, bucket),
],
[]
)
return minioClient.removeObjects(bucket, keys)
}
const addKeyIfIncludesPublicCustomDomain = (url: string) =>
env.S3_PUBLIC_CUSTOM_DOMAIN && url.includes(env.S3_PUBLIC_CUSTOM_DOMAIN)
? [url.split(env.S3_PUBLIC_CUSTOM_DOMAIN + '/')[1]]
: []
const addKeyIfIncludesDefaultEndpoint = (url: string, bucket: string) =>
url.includes(env.S3_ENDPOINT as string) ? [url.split(`/${bucket}/`)[1]] : []

View File

@ -1,5 +1,6 @@
import { env } from '@typebot.io/env'
import { Client, PostPolicyResult } from 'minio'
import { PostPolicyResult } from 'minio'
import { initClient } from './initClient'
type Props = {
filePath: string
@ -14,19 +15,7 @@ export const generatePresignedPostPolicy = async ({
fileType,
maxFileSize,
}: Props): Promise<PostPolicyResult> => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const minioClient = initClient()
const postPolicy = minioClient.newPostPolicy()
if (maxFileSize)

View File

@ -1,5 +1,5 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
import { initClient } from './initClient'
type Props = {
key: string
@ -9,19 +9,6 @@ export const getFileTempUrl = async ({
key,
expires,
}: Props): Promise<string> => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const minioClient = initClient()
return minioClient.presignedGetObject(env.S3_BUCKET, key, expires ?? 3600)
}

View File

@ -1,24 +1,12 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
import { initClient } from './initClient'
type Props = {
folderPath: string
}
export const getFolderSize = async ({ folderPath }: Props) => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const minioClient = initClient()
return new Promise<number>((resolve, reject) => {
let totalSize = 0

View File

@ -0,0 +1,20 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
export const initClient = () => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
return minioClient
}

View File

@ -0,0 +1,67 @@
import { env } from '@typebot.io/env'
import { initClient } from './initClient'
const removeObjectsRecursively = async (prefix: string) => {
const minioClient = initClient()
const bucketName = env.S3_BUCKET
const objectsStream = minioClient.listObjectsV2(bucketName, prefix, true)
for await (const obj of objectsStream) {
try {
await minioClient.removeObject(bucketName, obj.name)
} catch (err) {
console.error(`Error removing ${obj.name}:`, err)
}
}
}
export const removeObjectsFromWorkspace = async (workspaceId: string) => {
await removeObjectsRecursively(`public/workspaces/${workspaceId}/`)
await removeObjectsRecursively(`private/workspaces/${workspaceId}/`)
}
export const removeObjectsFromResult = async ({
workspaceId,
resultIds,
typebotId,
}: {
workspaceId: string
resultIds: string[]
typebotId: string
}) => {
for (const resultId of resultIds) {
await removeObjectsRecursively(
`public/workspaces/${workspaceId}/typebots/${typebotId}/results/${resultId}/`
)
}
}
export const removeAllObjectsFromResult = async ({
workspaceId,
typebotId,
}: {
workspaceId: string
typebotId: string
}) => {
await removeObjectsRecursively(
`public/workspaces/${workspaceId}/typebots/${typebotId}/results/`
)
}
export const removeObjectsFromTypebot = async ({
typebotId,
workspaceId,
}: {
typebotId: string
workspaceId: string
}) => {
await removeObjectsRecursively(
`public/workspaces/${workspaceId}/typebots/${typebotId}/`
)
}
export const removeObjectsFromUser = async (userId: string) => {
await removeObjectsRecursively(`public/users/${userId}/`)
}

View File

@ -1,5 +1,5 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
import { initClient } from './initClient'
type Props = {
key: string
@ -12,19 +12,7 @@ export const uploadFileToBucket = async ({
file,
mimeType,
}: Props): Promise<string> => {
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
throw new Error(
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const minioClient = new Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const minioClient = initClient()
await minioClient.putObject(env.S3_BUCKET, 'public/' + key, file, {
'Content-Type': mimeType,