-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (64 loc) · 2.29 KB
/
index.js
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
import { createReadStream, statSync, readdirSync, } from 'fs';
import { resolve } from 'path';
import { createRequire } from 'module';
import chalk from "chalk";
const require = createRequire(import.meta.url);
const log = require('single-line-log').stdout;
class AliOssUploader{
/**
* aliOssUploader
* @param {Object} aliossConfig
* @param {Object} uploadConfig
* @example { region: 'oss-cn-hangzhou', accessKeyId: 'your keyid', accessKeySecret: 'you keysecret', bucket: 'your bucket',}
* @example { dirpath: resolve(__dirname, './test'), destpath: 'your destpath belong to the current bucket'}
*/
constructor(aliossConfig, uploadConfig) {
const OSS = require('ali-oss');
this.client = new OSS(aliossConfig);
this.dirpath = uploadConfig.dirpath;
this.destpath = uploadConfig.destpath;
this.streamList = [];
this.streamAmount = 0;
}
start() {
this.streamFactory(this.dirpath, this.destpath);
this.streamList.forEach((fileItem, idx) => {
this.putStream(fileItem.destPath, fileItem.stream, idx);
});
}
/**
* aliyunoss 流式上传
* @param {String} filename 上传至oss使用的文件名
* @param {String} stream 可读的文件流
*/
async putStream (filename, stream, idx) {
try {
await this.client.putStream(filename, stream);
log(chalk.green(`Processing: ${idx + 1}/${this.streamAmount} ${filename}`));
(idx + 1 === this.streamAmount) && log(chalk.green(`上传完成,本次共计${this.streamAmount}个文件`));
} catch (err) {
throw err;
}
}
/**
* 生成stream流列表
* @param {String} dirpath 本地需要上传的目录位置
* @param {String} destPath 上传至服务器的目录位置
*/
streamFactory(dirpath, destPath) {
const files = readdirSync(dirpath);
files.length && files.forEach((filename) => {
const filepath = resolve(dirpath, filename);
const isDir = statSync(filepath).isDirectory();
const isFile = statSync(filepath).isFile();
if(isFile) {
const stream = createReadStream(filepath);
this.streamList.push({ stream, destPath: `${destPath}/${filename}` });
this.streamAmount++;
}else if (isDir) {
this.streamFactory(filepath, `${destPath}/${filename}`);
}
});
}
}
export default AliOssUploader;