-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss.ts
74 lines (67 loc) · 1.92 KB
/
oss.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const OSS = require('ali-oss')
const path = require('path')
const fs = require('fs')
console.log("start oss");
interface Config{
region: String,
accessKeyId: String,
accessKeySecret: String,
bucket:String
}
class Client extends OSS{
protected readonly entry:string;
constructor(config:Config,entry:string) {
super(config);
this.entry = entry
this.init()
}
protected searchFolder(folder:String,callback:(p:String)=>void):void{
if(typeof folder === 'undefined'){
folder = path.join(__dirname,'./public')
}
if(fs.statSync(folder).isFile()){
callback(folder)
return
}
fs.readdir(folder,(err,paths)=>{
for(let p of paths){
const lastP = path.join(folder,`${p}`)
fs.stat(lastP,(error, stats)=>{
if(!err){
if(stats.isFile()){
typeof callback === 'function' && callback(lastP)
}
else {
this.searchFolder(lastP,callback)
}
} else {
console.error(err)
}
})
}
})
}
public init():void{
this.searchFolder(entry,(p)=>{
const relativePath = path.relative(entry,p)
const lastP = '/' + relativePath.split(path.sep).join('/')
this.put(lastP, fs.createReadStream(p)).then((result) => {
console.log('done');
});
})
}
}
const entry = path.join(__dirname,'./public')
const {
BUCKET,
REGION,
ACCESSKEYSECRET,
ACCESSKEYID
} = process.env
console.log("start oss2",JSON.stringify(process.env));
new Client({
region: REGION,
accessKeyId: ACCESSKEYID,
accessKeySecret: ACCESSKEYSECRET,
bucket: BUCKET
},entry)