-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathminio-provider.ts
More file actions
95 lines (84 loc) · 2.56 KB
/
minio-provider.ts
File metadata and controls
95 lines (84 loc) · 2.56 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import fs from 'fs'
// eslint-disable-next-line import/no-extraneous-dependencies
import { S3 } from 'aws-sdk'
import { UploadedFile } from 'adminjs'
import { ERROR_MESSAGES, DAY_IN_MINUTES } from '../constants'
import { BaseProvider } from './base-provider'
/**
* AWS Credentials which can be set for S3 file upload.
* If not given, 'aws-sdk' will try to fetch them from
* environmental variables.
* @memberof module:@adminjs/upload
*/
export type MinIoOptions = {
/**
* MinIo endpoint
*/
endpoint: string;
/**
* AWS IAM accessKeyId. By default its value is taken from AWS_ACCESS_KEY_ID env variable
*/
accessKeyId?: string;
/**
* AWS IAM secretAccessKey. By default its value is taken from AWS_SECRET_ACCESS_KEY env variable
*/
secretAccessKey?: string;
/**
* S3 Bucket where files will be stored
*/
bucket: string;
/**
* indicates how long links should be available after page load (in minutes).
* Default to 24h. If set to 0 adapter will mark uploaded files as PUBLIC ACL.
*/
expires?: number;
}
export class MinIoProvider extends BaseProvider {
private s3: S3
private endpoint: string
public expires: number
constructor(options: MinIoOptions) {
super(options.bucket)
let AWS_S3: typeof S3
try {
// eslint-disable-next-line
const AWS = require('aws-sdk')
AWS_S3 = AWS.S3
} catch (error) {
throw new Error(ERROR_MESSAGES.NO_AWS_SDK)
}
this.expires = options.expires ?? DAY_IN_MINUTES
this.endpoint = options.endpoint
this.s3 = new AWS_S3({
s3ForcePathStyle: true, // needed with minio?
signatureVersion: 'v4',
...options })
}
public async upload(file: UploadedFile, key: string): Promise<S3.ManagedUpload.SendData> {
const uploadOptions = { partSize: 5 * 1024 * 1024, queueSize: 10 }
const tmpFile = fs.createReadStream(file.path)
const params: S3.PutObjectRequest = {
Bucket: this.bucket,
Key: key,
Body: tmpFile,
}
if (!this.expires) {
params.ACL = 'public-read'
}
return this.s3.upload(params, uploadOptions).promise()
}
public async delete(key: string, bucket: string): Promise<S3.DeleteObjectOutput> {
return this.s3.deleteObject({ Key: key, Bucket: bucket }).promise()
}
public async path(key: string, bucket: string): Promise<string> {
if (this.expires) {
return this.s3.getSignedUrl('getObject', {
Key: key,
Bucket: bucket,
Expires: this.expires,
})
}
// https://bucket.s3.amazonaws.com/key
return `${this.endpoint}/${bucket}/${key}`
}
}