1
- import { Client } from 'minio' ;
1
+ import { Client , type RemoveOptions , type CopyConditions , type LifecycleConfig } from 'minio' ;
2
2
import {
3
3
defaultS3Options ,
4
+ type CreatePostPresignedUrlOptions ,
4
5
type CreatePostPresignedUrlParams ,
5
6
type CreatePostPresignedUrlResult ,
6
7
type S3BucketName ,
7
8
type S3Options
8
9
} from '../types' ;
9
10
import type { IBucketBasicOperations } from '../interface' ;
10
- import { createObjectKey , createPresignedUrlExpires , inferContentType } from '../helpers' ;
11
+ import {
12
+ createObjectKey ,
13
+ createPresignedUrlExpires ,
14
+ createTempObjectKey ,
15
+ inferContentType
16
+ } from '../helpers' ;
11
17
12
18
export class S3BaseBucket implements IBucketBasicOperations {
13
19
public client : Client ;
@@ -16,11 +22,11 @@ export class S3BaseBucket implements IBucketBasicOperations {
16
22
*
17
23
* @param _bucket the bucket you want to operate
18
24
* @param options the options for the s3 client
19
- * @param afterInit the function to be called after instantiating the s3 service
25
+ * @param afterInits the function to be called after instantiating the s3 service
20
26
*/
21
27
constructor (
22
28
private readonly _bucket : S3BucketName ,
23
- private readonly afterInit ?: ( ) => Promise < void > | void ,
29
+ private readonly afterInits ?: ( ( ) => Promise < void > | void ) [ ] ,
24
30
public options : Partial < S3Options > = defaultS3Options
25
31
) {
26
32
options = { ...defaultS3Options , ...options } ;
@@ -31,45 +37,54 @@ export class S3BaseBucket implements IBucketBasicOperations {
31
37
if ( ! ( await this . exist ( ) ) ) {
32
38
await this . client . makeBucket ( this . _bucket ) ;
33
39
}
34
- await this . afterInit ?. ( ) ;
40
+ await Promise . all ( this . afterInits ?. map ( ( afterInit ) => afterInit ( ) ) ?? [ ] ) ;
35
41
} ;
36
42
init ( ) ;
37
43
}
38
44
39
- async exist ( ) : Promise < boolean > {
40
- return await this . client . bucketExists ( this . _bucket ) ;
41
- }
42
-
43
45
get name ( ) : string {
44
46
return this . _bucket ;
45
47
}
46
48
47
- upload ( ) : Promise < void > {
48
- throw new Error ( 'Method not implemented.' ) ;
49
+ async move ( src : string , dst : string , options ?: CopyConditions ) : Promise < void > {
50
+ const bucket = this . name ;
51
+ await this . client . copyObject ( bucket , dst , `/${ bucket } /${ src } ` , options ) ;
52
+ return this . client . removeObject ( bucket , src ) ;
49
53
}
50
54
51
- download ( ) : Promise < void > {
52
- throw new Error ( 'Method not implemented.' ) ;
55
+ copy ( src : string , dst : string , options ?: CopyConditions ) : ReturnType < Client [ 'copyObject' ] > {
56
+ return this . client . copyObject ( this . name , src , dst , options ) ;
53
57
}
54
58
55
- delete ( objectKey : string ) : Promise < void > {
56
- return this . client . removeObject ( this . _bucket , objectKey ) ;
59
+ exist ( ) : Promise < boolean > {
60
+ return this . client . bucketExists ( this . name ) ;
61
+ }
62
+
63
+ delete ( objectKey : string , options ?: RemoveOptions ) : Promise < void > {
64
+ return this . client . removeObject ( this . name , objectKey , options ) ;
57
65
}
58
66
59
67
get ( ) : Promise < void > {
60
68
throw new Error ( 'Method not implemented.' ) ;
61
69
}
62
70
71
+ lifecycle ( ) : Promise < LifecycleConfig | null > {
72
+ return this . client . getBucketLifecycle ( this . name ) ;
73
+ }
74
+
63
75
async createPostPresignedUrl (
64
- params : CreatePostPresignedUrlParams
76
+ params : CreatePostPresignedUrlParams ,
77
+ options : CreatePostPresignedUrlOptions = { }
65
78
) : Promise < CreatePostPresignedUrlResult > {
66
- const maxFileSize = this . options . maxFileSize as number ;
79
+ const { temporay } = options ;
67
80
const contentType = inferContentType ( params . filename ) ;
81
+ const maxFileSize = this . options . maxFileSize as number ;
82
+ const key = temporay ? createTempObjectKey ( params ) : createObjectKey ( params ) ;
68
83
69
84
const policy = this . client . newPostPolicy ( ) ;
70
- policy . setBucket ( this . _bucket ) ;
85
+ policy . setKey ( key ) ;
86
+ policy . setBucket ( this . name ) ;
71
87
policy . setContentType ( contentType ) ;
72
- policy . setKey ( createObjectKey ( params ) ) ;
73
88
policy . setContentLengthRange ( 1 , maxFileSize ) ;
74
89
policy . setExpires ( createPresignedUrlExpires ( 10 ) ) ;
75
90
policy . setUserMetaData ( {
0 commit comments