import * as dateFns from 'date-fns' ; import { NosBaseClient } from '../client' ; import { NosError } from '../lib/error' ; import { Callbackable , normalizeArray } from '../lib/util' ; import { Bucket , BucketAcl , BucketLocation , ListBucketResult , OperateBucketParams , PutBucketParams , SetBucketAclParams , } from '../type/bucket' ; import { Callback } from '../type/callback' ; import { ResourceBucket } from '../type/resource' ; import { pick } from 'ramda' ; export class NosClientBucketExt extends NosBaseClient { /**
     * 获取全部 Bucket
     */ listBucket ( ) ; listBucket ( cb ) ; @ Callbackable async listBucket ( ) { const data = await this . requestBody ( 'get' , { host : `${ this . options . host }` , } , { } ) ; let buckets = normalizeArray ( data . listAllMyBucketsResult . buckets . bucket ) ; for ( const bkt of buckets ) { bkt . creationDate = dateFns . parse ( bkt . creationDate ) ; } return { ... ( pick ( [ 'owner' ] , data . listAllMyBucketsResult ) as any ) , items : buckets , isTruncated : false , limit : buckets . length , nextMarker : '' , } ; } param params param params putBucket ( params ) ; putBucket ( params , cb ) ; @ Callbackable async putBucket ( params ) { const { headers , resource } = this . validateParams ( params ) ; if ( params . acl ) { headers [ 'x-nos-acl' ] = params . acl ; } await this . requestBody ( 'put' , headers , resource , { createBucketConfiguration : { locationConstraint : params . location || BucketLocation . HZ , } , } ) ; } /**
     * 确保 Bucket 存在。如果不存在，会自动创建，如果存在，不做任何操作。
     */ ensureBucket ( params ) ; ensureBucket ( params , cb ) ; @ Callbackable async ensureBucket ( params ) { try { return await this . putBucket ( params ) ; } catch ( e ) { if ( e instanceof NosError && e . name === 'BucketAlreadyOwnedByYou' ) { return ; } throw e ; } } /**
     * 检查一个 Bucket 是否存在
     */ isBucketExist ( params ) ; isBucketExist ( params , cb ) ; @ Callbackable async isBucketExist ( params ) { const { headers , resource } = this . validateParams ( params ) ; const resp = await this . _request ( 'head' , headers , resource ) ; if ( resp . status === 404 ) return false ; await this . handleRequestError ( resp ) ; return true ; } /**
     * 删除 Bucket
     */ deleteBucket ( params ) ; deleteBucket ( params , cb ) ; @ Callbackable async deleteBucket ( params ) { const { headers , resource } = this . validateParams ( params ) ; try { await this . request ( 'delete' , headers , resource ) ; } catch ( e ) { if ( e instanceof NosError && e . name === 'NoSuchBucket' ) { return ; } throw e ; } } /**
     * 获取 Bucket 的权限
     */ getBucketAcl ( params ) ; getBucketAcl ( params , cb ) ; @ Callbackable async getBucketAcl ( params ) { const { headers , resource } = this . validateParams ( params ) ; ( resource as ResourceBucket ) . acl = true ; const resp = await this . request ( 'get' , headers , resource ) ; return resp . headers . get ( 'x-nos-acl' ) as BucketAcl ; } /**
     * 设置 Bucket 的权限
     */ setBucketAcl ( params ) ; setBucketAcl ( params , cb ) ; @ Callbackable async setBucketAcl ( params ) { const { headers , resource } = this . validateParams ( params ) ; Object . assign ( resource , { acl : true } ) ; headers [ 'x-nos-acl' ] = params . acl ; await this . request ( 'put' , headers , resource ) ; } /**
     * 获取 Bucket 的地域位置
     */ getBucketLocation ( params ) ; getBucketLocation ( params , cb ) ; @ Callbackable async getBucketLocation ( params ) { const { headers , resource } = this . validateParams ( params ) ; Object . assign ( resource , { location : true } ) ; const result = await this . requestBody ( 'get' , headers , resource ) ; return result . locationConstraint ; } }