Skip to content

Commit

Permalink
fix(oss): fix oss api error;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Apr 14, 2022
1 parent cb86567 commit 0fe8c90
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
30 changes: 24 additions & 6 deletions packages/system-server/src/api/oss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert = require("assert")
import * as cp from 'child_process'
import { promisify } from 'util'
import { STSClient, AssumeRoleCommand } from '@aws-sdk/client-sts'
import { CreateBucketCommand, DeleteBucketPolicyCommand, PutBucketPolicyCommand, S3 } from '@aws-sdk/client-s3'
import { CreateBucketCommand, DeleteBucketCommand, DeleteBucketPolicyCommand, PutBucketPolicyCommand, S3 } from '@aws-sdk/client-s3'
import { logger } from "../lib/logger"
import Config from "../config"
import { ApplicationStruct } from "./application"
Expand Down Expand Up @@ -36,9 +36,10 @@ export class MinioAgent {
return ma
}

constructor() {
throw new Error('constructor() is unsupported, use `await MinioAgent.New()` instead')
}
/**
* @deprecated constructor() is unsupported, use `await MinioAgent.New()` instead
*/
constructor() { }

/**
* Create s3 client
Expand Down Expand Up @@ -132,6 +133,9 @@ export class MinioAgent {
const s3 = this.getClient()
const cmd = new CreateBucketCommand({ Bucket: name, ACL: acl })
const res = await s3.send(cmd)
if (res?.$metadata?.httpStatusCode === 200) {
await this.setBucketACL(name, acl)
}

return res
}
Expand Down Expand Up @@ -168,10 +172,24 @@ export class MinioAgent {
assert.ok(name, 'empty name got')

const s3 = this.getClient()
const cmd = new DeleteBucketPolicyCommand({ Bucket: name })
const cmd = new DeleteBucketCommand({ Bucket: name })
return await s3.send(cmd)
}

/**
* Create an user policy
* @param policy_name
* @param policy_json_path
* @returns
*/
public async createUserPolicy(policy_name: string, policy_json_path: string) {
assert.ok(policy_name, 'empty policy_name got')
assert.ok(policy_json_path, 'empty policy_json_path got')

const sub_cmd = `admin policy add ${MinioAgent.MC_TARGET} ${policy_name} ${policy_json_path}`
return await this.mc_exec(sub_cmd)
}


/**
* Execute minio client shell
Expand All @@ -180,7 +198,7 @@ export class MinioAgent {
*/
private async mc_exec(sub_cmd: string) {
const mc_path = process.env.MINIO_CLIENT_PATH || 'mc'
const cmd = `${mc_path} ${sub_cmd} --json --anonymous`
const cmd = `${mc_path} ${sub_cmd} --json`

try {
const { stdout } = await exec(cmd)
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/router/oss/add-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export async function handleCreateBucket(req: Request, res: Response) {
return res.status(200).send({ code: 'EXISTED', error: 'bucket name already existed' })
}

const oss = new MinioAgent()
const internalName = `${app.appid}_${bucketName}`
const oss = await MinioAgent.New()
const internalName = `${app.appid}-${bucketName}`
const ret = await oss.createBucket(internalName, mode)
if (!ret) {
return res.status(400).send('create bucket failed')
Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/router/oss/delete-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export async function handleDeleteBucket(req: Request, res: Response) {
return res.status(code).send()
}

const oss = new MinioAgent()
const internalName = `${app.appid}_${bucketName}`
const oss = await MinioAgent.New()
const internalName = `${app.appid}-${bucketName}`
const ret = await oss.deleteBucket(internalName)
if (ret?.$metadata?.httpStatusCode !== 200) {
if (ret?.$metadata?.httpStatusCode !== 204) {
return res.send(ret?.$metadata)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/system-server/src/router/oss/get-buckets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function handleGetOneBucket(req: Request, res: Response) {

// bucket tokens
const exp = 60 * 60 * Config.TOKEN_EXPIRED_TIME
const oss = new MinioAgent()
const oss = await MinioAgent.New()
const sts = await oss.getApplicationSTS(app, exp)
const data = {
name,
Expand Down
5 changes: 2 additions & 3 deletions packages/system-server/src/router/oss/update-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { checkPermission } from '../../api/permission'
import { Constants } from '../../constants'
import { StorageAgent } from '../../api/storage'
import { DatabaseAgent } from '../../lib/db-agent'
import { BUCKET_ACL, MinioAgent } from '../../api/oss'

Expand Down Expand Up @@ -39,8 +38,8 @@ export async function handleSetBucketPolicy(req: Request, res: Response) {
return res.status(404).send('bucket not found')
}

const oss = new MinioAgent()
const internalName = `${app.appid}_${bucketName}`
const oss = await MinioAgent.New()
const internalName = `${app.appid}-${bucketName}`
const ret = await oss.setBucketACL(internalName, mode)
if (ret?.$metadata?.httpStatusCode !== 200) {
return res.send(ret?.$metadata)
Expand Down

0 comments on commit 0fe8c90

Please sign in to comment.