forked from sysucats/zhongdamaopu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetURL.ts
52 lines (46 loc) · 1.32 KB
/
getURL.ts
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
import cloud from '@lafjs/cloud'
import { getAppSecret } from "@/getAppSecret"
import * as Minio from 'minio';
export default async function (ctx: FunctionContext) {
const { body } = ctx
if (body && body.deploy_test === true) {
// 进行部署检查
return "v1.1";
}
const { fileName } = ctx.body;
return await signUrl(fileName);
}
// 签名方法
async function signUrl(fileName: string) {
// minIO 配置
const { OSS_ENDPOINT, OSS_PORT, OSS_BUCKET, OSS_SECRET_ID, OSS_SECRET_KEY } = await getAppSecret(false);
// 报错"Invalid endPoint"请参考: https://blog.csdn.net/xinleicol/article/details/115698599
const minioClient = {
endPoint: OSS_ENDPOINT,
port: OSS_PORT,
useSSL: true,
accessKey: OSS_SECRET_ID,
secretKey: OSS_SECRET_KEY,
pathStyle: false,
}
if (OSS_ENDPOINT =="oss.laf.run"){
minioClient.pathStyle = true
}
const client = new Minio.Client(minioClient);
return new Promise((resolve, reject) => {
let policy = client.newPostPolicy()
policy.setBucket(OSS_BUCKET)
policy.setKey(fileName)
let expires = new Date()
// 10d
expires.setSeconds(24 * 60 * 60 * 10);
policy.setExpires(expires)
client.presignedPostPolicy(policy, function (err, data) {
if (err) {
reject(err);
return
}
resolve(data)
})
})
}