-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathminio.js
More file actions
62 lines (51 loc) · 1.44 KB
/
minio.js
File metadata and controls
62 lines (51 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Client as Minio } from 'minio'
import retry from 'p-retry'
import { isPortReachable } from '../utils.js'
function minioConfig() {
const port = process.env.MINIO_API_PORT
? Number.parseInt(process.env.MINIO_API_PORT)
: 9000
return {
useSSL: false,
endPoint: '127.0.0.1',
port: port,
accessKey: 'minioadmin',
secretKey: 'minioadmin',
}
}
/**
* @param {string} name Bucket name
*/
export async function minioBucketCreateCmd(name) {
const config = minioConfig()
await retry(async () => {
if (!(await isPortReachable(config.port))) {
throw new Error(`Minio API not reachable on port: ${config.port}`)
}
})
const minio = new Minio(config)
if (await minio.bucketExists(name)) {
return console.log(`Cannot create bucket "${name}": already exists`)
}
await minio.makeBucket(name, 'us-east-1')
console.log(`Created bucket "${name}"`)
}
/**
* @param {string} name Bucket name
*/
export async function minioBucketRemoveCmd(name) {
const minio = new Minio(minioConfig())
if (!(await minio.bucketExists(name))) {
return console.log(`Cannot remove bucket "${name}": not found`)
}
const keys = []
for await (const item of minio.listObjectsV2(name, '', true)) {
keys.push(item.name)
}
if (keys.length) {
console.log(`Removing ${keys.length} items...`)
await minio.removeObjects(name, keys)
}
await minio.removeBucket(name)
console.log(`Removed bucket "${name}"`)
}