2023-03-02 19:21:45 +01:00
|
|
|
import { objectStore } from "@budibase/backend-core"
|
|
|
|
import fs from "fs"
|
|
|
|
import { join } from "path"
|
|
|
|
import { TEMP_DIR, MINIO_DIR } from "./utils"
|
|
|
|
import { progressBar } from "../utils"
|
2022-06-30 21:26:49 +02:00
|
|
|
const {
|
|
|
|
ObjectStoreBuckets,
|
|
|
|
ObjectStore,
|
|
|
|
retrieve,
|
|
|
|
uploadDirectory,
|
|
|
|
makeSureBucketExists,
|
2022-11-23 19:25:20 +01:00
|
|
|
} = objectStore
|
2022-06-30 21:26:49 +02:00
|
|
|
|
|
|
|
const bucketList = Object.values(ObjectStoreBuckets)
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function exportObjects() {
|
2022-06-30 21:26:49 +02:00
|
|
|
const path = join(TEMP_DIR, MINIO_DIR)
|
|
|
|
fs.mkdirSync(path)
|
2023-03-02 19:21:45 +01:00
|
|
|
let fullList: any[] = []
|
2022-10-26 19:51:58 +02:00
|
|
|
let errorCount = 0
|
2022-06-30 21:26:49 +02:00
|
|
|
for (let bucket of bucketList) {
|
|
|
|
const client = ObjectStore(bucket)
|
|
|
|
try {
|
|
|
|
await client.headBucket().promise()
|
|
|
|
} catch (err) {
|
2022-10-26 19:51:58 +02:00
|
|
|
errorCount++
|
2022-06-30 21:26:49 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-03-02 19:21:45 +01:00
|
|
|
const list = (await client.listObjectsV2().promise()) as { Contents: any[] }
|
2022-06-30 21:26:49 +02:00
|
|
|
fullList = fullList.concat(list.Contents.map(el => ({ ...el, bucket })))
|
|
|
|
}
|
2022-10-26 19:51:58 +02:00
|
|
|
if (errorCount === bucketList.length) {
|
|
|
|
throw new Error("Unable to access MinIO/S3 - check environment config.")
|
|
|
|
}
|
2022-06-30 21:26:49 +02:00
|
|
|
const bar = progressBar(fullList.length)
|
|
|
|
let count = 0
|
|
|
|
for (let object of fullList) {
|
|
|
|
const filename = object.Key
|
|
|
|
const data = await retrieve(object.bucket, filename)
|
|
|
|
const possiblePath = filename.split("/")
|
|
|
|
if (possiblePath.length > 1) {
|
|
|
|
const dirs = possiblePath.slice(0, possiblePath.length - 1)
|
|
|
|
fs.mkdirSync(join(path, object.bucket, ...dirs), { recursive: true })
|
|
|
|
}
|
|
|
|
fs.writeFileSync(join(path, object.bucket, ...possiblePath), data)
|
|
|
|
bar.update(++count)
|
|
|
|
}
|
|
|
|
bar.stop()
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function importObjects() {
|
2022-06-30 21:26:49 +02:00
|
|
|
const path = join(TEMP_DIR, MINIO_DIR)
|
|
|
|
const buckets = fs.readdirSync(path)
|
|
|
|
let total = 0
|
|
|
|
buckets.forEach(bucket => {
|
|
|
|
const files = fs.readdirSync(join(path, bucket))
|
|
|
|
total += files.length
|
|
|
|
})
|
|
|
|
const bar = progressBar(total)
|
|
|
|
let count = 0
|
|
|
|
for (let bucket of buckets) {
|
|
|
|
const client = ObjectStore(bucket)
|
|
|
|
await makeSureBucketExists(client, bucket)
|
|
|
|
const files = await uploadDirectory(bucket, join(path, bucket), "/")
|
|
|
|
count += files.length
|
|
|
|
bar.update(count)
|
|
|
|
}
|
|
|
|
bar.stop()
|
|
|
|
}
|