Skip to content

Commit

Permalink
feat(oss): add bucket quota;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Apr 21, 2022
1 parent 4d9133b commit 2918104
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/system-server/src/api/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class ApplicationImporter {

const oss = await MinioAgent.New()
const internalName = `${this.app.appid}-${name}`
const ret = await oss.createBucket(internalName, mode)
const ret = await oss.createBucket(internalName, { acl: mode })
if (!ret) {
throw new Error(`Failed to create bucket: ${name}`)
}
Expand Down
57 changes: 51 additions & 6 deletions packages/system-server/src/api/oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,28 @@ export class MinioAgent {

/**
* Create bucket
* @param name bucket name
* @param acl bucket mode
* @returns bucket id or undefined
* @param name
* @param options
* @returns
*/
public async createBucket(name: string, acl: BUCKET_ACL) {
public async createBucket(name: string, options: { acl?: BUCKET_ACL, with_lock?: boolean, quota?: number }) {
assert.ok(name, 'empty name got')
assert.ok(acl !== undefined, 'undefined mode got')

const acl = options.acl || BUCKET_ACL.private
const s3 = this.getClient()
const cmd = new CreateBucketCommand({ Bucket: name, ACL: acl })
const cmd = new CreateBucketCommand({
Bucket: name,
ACL: acl,
CreateBucketConfiguration: {
LocationConstraint: Config.MINIO_CONFIG.region
},
ObjectLockEnabledForBucket: options?.with_lock ?? false
})
const res = await s3.send(cmd)
if (res?.$metadata?.httpStatusCode === 200) {
await this.setBucketACL(name, acl)
const quota = options?.quota || 1024 * 1024
await this.setBucketQuota(name, quota)
}

return res
Expand Down Expand Up @@ -163,6 +172,42 @@ export class MinioAgent {
return await s3.send(cmd)
}

/**
* Set bucket quota
* @param name the bucket name
* @param quota bucket quota size in bytes
*/
public async setBucketQuota(name: string, quota: number) {
assert.ok(name, 'empty name got')

const sub_cmd = `admin bucket quota ${MinioAgent.MC_TARGET}/${name} --hard ${quota}`
return await this.mc_exec(sub_cmd)
}

/**
* Get bucket quota
* @param name
* @returns
*/
public async getBucketQuota(name: string) {
assert.ok(name, 'empty name got')

const sub_cmd = `admin bucket quota ${MinioAgent.MC_TARGET}/${name}`
return await this.mc_exec(sub_cmd)
}

/**
* Clear bucket quota
* @param name
* @returns
*/
public async clearBucketQuota(name: string) {
assert.ok(name, 'empty name got')

const sub_cmd = `admin bucket quota ${MinioAgent.MC_TARGET}/${name} --clear`
return await this.mc_exec(sub_cmd)
}

/**
* Delete bucket
* @param name bucket name
Expand Down
5 changes: 5 additions & 0 deletions packages/system-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ export const Constants = {
}

deepFreeze(Constants)


export const KB = 1024
export const MB = 1024 * KB
export const GB = 1024 * MB
7 changes: 5 additions & 2 deletions packages/system-server/src/routes/oss/add-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { Constants, GB } from '../../constants'
import { DatabaseAgent } from '../../db'
import { BUCKET_ACL, MinioAgent } from '../../api/oss'

Expand All @@ -26,6 +26,9 @@ export async function handleCreateBucket(req: Request, res: Response) {
return res.status(422).send('invalid bucket mode')
}

let quota: number = req.body?.quota || 0
if (quota <= 0) { quota = 1 * GB }

const uid = req['auth']?.uid
const app: ApplicationStruct = req['parsed-app']

Expand All @@ -44,7 +47,7 @@ export async function handleCreateBucket(req: Request, res: Response) {

const oss = await MinioAgent.New()
const internalName = `${app.appid}-${bucketName}`
const ret = await oss.createBucket(internalName, mode)
const ret = await oss.createBucket(internalName, { acl: mode, quota, with_lock: false })
if (!ret) {
return res.status(400).send('create bucket failed')
}
Expand Down

0 comments on commit 2918104

Please sign in to comment.