-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
123 lines (107 loc) · 3.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { OSSObject } = require('oss-client');
function trimKey(key) {
key = key ? key.replace(/^\//, '') : '';
// %3A => :
key = key && key.indexOf('%3A') >= 0 ? decodeURIComponent(key) : key;
return key;
}
function autoFixInternalOSSUrl(url) {
// get from endpoint, https://some-bucket.region-name-internal.aliyuncs.com/foo/url => https://some-bucket.region-name.aliyuncs.com/foo/url
return url.replace('-internal.aliyuncs.com/', '.aliyuncs.com/');
}
class OssWrapper {
constructor(options) {
// If you want to use oss public mode, please set `options.mode = 'public'`
this._mode = options.mode === 'public' ? 'public' : 'private';
this.client = new OSSObject(options);
this._cdnBaseUrl = options.cdnBaseUrl;
this._defaultHeaders = options.defaultHeaders;
}
async upload(filePath, options) {
const key = trimKey(options.key);
// https://github.com/node-modules/oss-client#putname-file-options
const result = await this.client.put(key, filePath, {
headers: this._defaultHeaders,
});
if (this._mode === 'public') {
return { url: result.url };
}
return { key };
}
async uploadBytes(bytes, options) {
if (typeof bytes === 'string') {
bytes = Buffer.from(bytes);
}
return await this.upload(bytes, options);
}
// options.position, default is '0'
async appendBytes(bytes, options) {
// nextAppendPosition on result
const key = trimKey(options.key);
if (typeof bytes === 'string') {
bytes = Buffer.from(bytes);
}
return await this.client.append(key, bytes, options);
}
async readBytes(key) {
const { content } = await this.client.get(trimKey(key));
return content;
}
async download(key, filepath, options) {
await this.client.get(trimKey(key), filepath, options);
}
/**
* @param {string} prefix - file prefix
* @param {object} [options] -
* @param {number} [options.max] - default 10000
* @return {Generator<string[]>} -
*/
async list(prefix, options) {
const max = options && options.max || 10000;
const stepMax = Math.min(1000, max);
let marker = null;
let files = [];
do {
const res = await this.client.list({
prefix,
'max-keys': stepMax,
marker,
});
const objects = res.objects || [];
const prefixLength = prefix.length;
const nextFiles = objects.map(o => o.name.substring(prefixLength));
files = files.concat(nextFiles);
marker = res.nextMarker;
} while (marker && files.length <= max);
return files;
}
async createDownloadStream(key, options) {
return (await this.client.getStream(trimKey(key), options)).stream;
}
async url(key, options) {
const name = trimKey(key);
if (this._cdnBaseUrl) {
return this.client.getObjectUrl(name, this._cdnBaseUrl);
}
return autoFixInternalOSSUrl(this.client.signatureUrl(name, options));
}
async urls(key, options) {
const name = trimKey(key);
let cdnUrl;
if (this._cdnBaseUrl) {
cdnUrl = this.client.getObjectUrl(name, this._cdnBaseUrl);
}
const urls = [];
if (urls.length === 0) {
urls.push(autoFixInternalOSSUrl(this.client.signatureUrl(name, options)));
}
if (cdnUrl) {
urls.unshift(cdnUrl);
}
return urls;
}
async remove(key) {
await this.client.delete(trimKey(key));
}
}
module.exports = OssWrapper;