dfs-adapter 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/cos.d.ts +14 -0
- package/lib/cos.js +86 -0
- package/lib/declare.d.ts +17 -1
- package/lib/declare.js +1 -1
- package/lib/obs.d.ts +14 -0
- package/lib/obs.js +92 -0
- package/package.json +43 -39
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
###
|
|
1
|
+
###
|
package/lib/cos.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Dfs, CosConfig } from "./declare";
|
|
3
|
+
import { VWebApplicationContext } from "vweb-core";
|
|
4
|
+
export default class CosDfs extends Dfs<'cos'> {
|
|
5
|
+
private readonly client;
|
|
6
|
+
constructor(options: CosConfig & {
|
|
7
|
+
type: 'cos';
|
|
8
|
+
}, context: VWebApplicationContext);
|
|
9
|
+
startup(): Promise<void>;
|
|
10
|
+
private getKey;
|
|
11
|
+
upload(path: string, buffer: any, options?: any): Promise<string>;
|
|
12
|
+
download(path: string, options?: any): Promise<Buffer>;
|
|
13
|
+
getAccess(path: string, options?: any): Promise<string>;
|
|
14
|
+
}
|
package/lib/cos.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const declare_1 = require("./declare");
|
|
7
|
+
const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
class CosDfs extends declare_1.Dfs {
|
|
10
|
+
client;
|
|
11
|
+
constructor(options, context) {
|
|
12
|
+
super(options, context);
|
|
13
|
+
this.client = new cos_nodejs_sdk_v5_1.default({
|
|
14
|
+
SecretId: options.accessKeyId,
|
|
15
|
+
SecretKey: options.accessKeySecret
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async startup() {
|
|
19
|
+
return super.startup();
|
|
20
|
+
}
|
|
21
|
+
getKey(path) {
|
|
22
|
+
if (path.startsWith('/')) {
|
|
23
|
+
path = path.substring(1);
|
|
24
|
+
}
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
async upload(path, buffer, options = {}) {
|
|
28
|
+
try {
|
|
29
|
+
const Key = this.getKey(path);
|
|
30
|
+
this.timeBegin(`upload ${path}`);
|
|
31
|
+
let payload = { Body: buffer, ...options };
|
|
32
|
+
if (typeof buffer === 'string' && fs_1.default.existsSync(buffer)) {
|
|
33
|
+
payload = {
|
|
34
|
+
Body: fs_1.default.createReadStream(path),
|
|
35
|
+
ContentLength: fs_1.default.statSync(path).size
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
await this.client.putObject({
|
|
39
|
+
Bucket: this.options.bucket,
|
|
40
|
+
Region: this.options.region,
|
|
41
|
+
Key,
|
|
42
|
+
...payload
|
|
43
|
+
});
|
|
44
|
+
return Key;
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
this.timeEnd(`upload ${path}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async download(path, options = {}) {
|
|
51
|
+
try {
|
|
52
|
+
const Key = this.getKey(path);
|
|
53
|
+
this.timeBegin(`download ${path}`);
|
|
54
|
+
const result = await this.client.getObject({
|
|
55
|
+
Bucket: this.options.bucket,
|
|
56
|
+
Region: this.options.region,
|
|
57
|
+
Key,
|
|
58
|
+
...options
|
|
59
|
+
});
|
|
60
|
+
return result.Body;
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
this.timeEnd(`download ${path}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async getAccess(path, options = {}) {
|
|
67
|
+
if (this.options.access) {
|
|
68
|
+
return this.options.access + path;
|
|
69
|
+
}
|
|
70
|
+
const Key = this.getKey(path);
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
this.client.getObjectUrl({
|
|
73
|
+
Bucket: this.options.bucket,
|
|
74
|
+
Region: this.options.region,
|
|
75
|
+
Key,
|
|
76
|
+
...options
|
|
77
|
+
}, (err, result) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
return reject(err);
|
|
80
|
+
}
|
|
81
|
+
resolve(result.Url);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.default = CosDfs;
|
package/lib/declare.d.ts
CHANGED
|
@@ -45,12 +45,28 @@ export declare interface FastdfsConfig {
|
|
|
45
45
|
token: string;
|
|
46
46
|
access: string;
|
|
47
47
|
}
|
|
48
|
+
export declare interface ObsConfig {
|
|
49
|
+
accessKeyId: string;
|
|
50
|
+
accessKeySecret: string;
|
|
51
|
+
endpoint: string;
|
|
52
|
+
bucket: string;
|
|
53
|
+
access: string;
|
|
54
|
+
}
|
|
55
|
+
export declare interface CosConfig {
|
|
56
|
+
accessKeyId: string;
|
|
57
|
+
accessKeySecret: string;
|
|
58
|
+
region: string;
|
|
59
|
+
bucket: string;
|
|
60
|
+
access?: string;
|
|
61
|
+
}
|
|
48
62
|
export declare type ConfigOptionsMap = {
|
|
49
63
|
oss: OssConfig;
|
|
50
64
|
minio: MinioConfig;
|
|
51
65
|
http: HttpConfig;
|
|
52
66
|
native: NativeConfig;
|
|
53
67
|
fastdfs: FastdfsConfig;
|
|
68
|
+
obs: ObsConfig;
|
|
69
|
+
cos: CosConfig;
|
|
54
70
|
};
|
|
55
71
|
export declare type ConfigOptionsKey = keyof ConfigOptionsMap;
|
|
56
72
|
type ConfigOptionsOptions<T> = T extends ConfigOptionsKey ? {
|
|
@@ -71,6 +87,6 @@ export declare class Dfs<T extends ConfigOptionsKey> {
|
|
|
71
87
|
startup(): Promise<void>;
|
|
72
88
|
upload(path: string, buffer: any, ...args: any[]): Promise<string>;
|
|
73
89
|
download(path: string, ...args: any[]): Promise<Buffer>;
|
|
74
|
-
getAccess(path: string): Promise<string>;
|
|
90
|
+
getAccess(path: string, ...args: any[]): Promise<string>;
|
|
75
91
|
}
|
|
76
92
|
export {};
|
package/lib/declare.js
CHANGED
package/lib/obs.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Dfs, ObsConfig } from "./declare";
|
|
3
|
+
import { VWebApplicationContext } from "vweb-core";
|
|
4
|
+
export default class ObsDfs extends Dfs<'obs'> {
|
|
5
|
+
private readonly client;
|
|
6
|
+
constructor(options: ObsConfig & {
|
|
7
|
+
type: 'obs';
|
|
8
|
+
}, context: VWebApplicationContext);
|
|
9
|
+
startup(): Promise<void>;
|
|
10
|
+
private getKey;
|
|
11
|
+
upload(path: string, buffer: any, options?: any): Promise<string>;
|
|
12
|
+
download(path: string, options?: any): Promise<Buffer>;
|
|
13
|
+
getAccess(path: string, options?: any): Promise<string>;
|
|
14
|
+
}
|
package/lib/obs.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const declare_1 = require("./declare");
|
|
7
|
+
const esdk_obs_nodejs_1 = __importDefault(require("esdk-obs-nodejs"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
class ObsDfs extends declare_1.Dfs {
|
|
10
|
+
client;
|
|
11
|
+
constructor(options, context) {
|
|
12
|
+
super(options, context);
|
|
13
|
+
this.client = new esdk_obs_nodejs_1.default({
|
|
14
|
+
access_key_id: options.accessKeyId,
|
|
15
|
+
secret_access_key: options.accessKeySecret,
|
|
16
|
+
server: options.endpoint
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async startup() {
|
|
20
|
+
return super.startup();
|
|
21
|
+
}
|
|
22
|
+
getKey(path) {
|
|
23
|
+
if (path.startsWith('/')) {
|
|
24
|
+
path = path.substring(1);
|
|
25
|
+
}
|
|
26
|
+
return path;
|
|
27
|
+
}
|
|
28
|
+
async upload(path, buffer, options = {}) {
|
|
29
|
+
try {
|
|
30
|
+
const Key = this.getKey(path);
|
|
31
|
+
this.timeBegin(`upload ${path}`);
|
|
32
|
+
let payload = { Bucket: this.options.bucket, Key, ...options };
|
|
33
|
+
if (typeof buffer === 'string' && fs_1.default.existsSync(buffer)) {
|
|
34
|
+
payload.SourceFile = buffer;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
payload.Body = buffer;
|
|
38
|
+
}
|
|
39
|
+
await new Promise((resolve, reject) => {
|
|
40
|
+
this.client.putObject(payload, (err, result) => {
|
|
41
|
+
if (err) {
|
|
42
|
+
return reject(err);
|
|
43
|
+
}
|
|
44
|
+
this.logger.debug(`文件【${Key}】上传完成, Status:${result.CommonMsg.Status} Code: ${result.CommonMsg.Code} Message: ${result.CommonMsg.Message}`);
|
|
45
|
+
resolve(result);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
return Key;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this.timeEnd(`upload ${path}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async download(path, options = {}) {
|
|
55
|
+
try {
|
|
56
|
+
const Key = this.getKey(path);
|
|
57
|
+
this.timeBegin(`download ${path}`);
|
|
58
|
+
return await new Promise((resolve, reject) => {
|
|
59
|
+
this.client.getObject({
|
|
60
|
+
Bucket: this.options.bucket,
|
|
61
|
+
Key,
|
|
62
|
+
SaveAsStream: true,
|
|
63
|
+
...options
|
|
64
|
+
}, (err, result) => {
|
|
65
|
+
if (err) {
|
|
66
|
+
return reject(err);
|
|
67
|
+
}
|
|
68
|
+
this.logger.debug(`文件【${Key}】下载完成, Status:${result.CommonMsg.Status} Code: ${result.CommonMsg.Code} Message: ${result.CommonMsg.Message}`);
|
|
69
|
+
if (result.CommonMsg.Status < 300 && result.InterfaceResult) {
|
|
70
|
+
console.log(result);
|
|
71
|
+
result.InterfaceResult.Content.on('data', resolve);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
reject(result.CommonMsg.Status);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
this.timeEnd(`download ${path}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async getAccess(path, options = {}) {
|
|
84
|
+
if (this.options.access) {
|
|
85
|
+
return this.options.access + path;
|
|
86
|
+
}
|
|
87
|
+
const Key = this.getKey(path);
|
|
88
|
+
const { SignedUrl } = this.client.createSignedUrlSync({ Method: "GET", Bucket: this.options.bucket, Key, Expires: 86400, ...options });
|
|
89
|
+
return SignedUrl;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.default = ObsDfs;
|
package/package.json
CHANGED
|
@@ -1,39 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dfs-adapter",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "oss minio fastdfs native",
|
|
5
|
-
"main": "./lib/index",
|
|
6
|
-
"files": [
|
|
7
|
-
"lib/"
|
|
8
|
-
],
|
|
9
|
-
"scripts": {
|
|
10
|
-
"test": "node bin/app.js",
|
|
11
|
-
"build": "tsc --build tsconfig.json",
|
|
12
|
-
"run": "npm run build && node bin/app.js"
|
|
13
|
-
},
|
|
14
|
-
"repository": {
|
|
15
|
-
"type": "git",
|
|
16
|
-
"url": "git@codeup.aliyun.com:60b0836844816b8ed2335ed6/vweb/modules/mq-adapter.git"
|
|
17
|
-
},
|
|
18
|
-
"devDependencies": {
|
|
19
|
-
"@babel/cli": "^7.16.0",
|
|
20
|
-
"@babel/core": "^7.16.0",
|
|
21
|
-
"@babel/plugin-proposal-decorators": "^7.16.4",
|
|
22
|
-
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
23
|
-
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
24
|
-
"@babel/preset-env": "^7.16.4",
|
|
25
|
-
"@babel/register": "^7.16.0",
|
|
26
|
-
"@babel/runtime": "^7.16.3",
|
|
27
|
-
"@types/ali-oss": "^6.16.3",
|
|
28
|
-
"@types/node": "^16.11.12",
|
|
29
|
-
"ali-oss": "^6.18.1",
|
|
30
|
-
"axios": "^1.6.1",
|
|
31
|
-
"babel-plugin-transform-decorators-legacy": "^1.3.5",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"vweb-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "dfs-adapter",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "oss minio fastdfs native",
|
|
5
|
+
"main": "./lib/index",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib/"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node bin/app.js",
|
|
11
|
+
"build": "tsc --build tsconfig.json",
|
|
12
|
+
"run": "npm run build && node bin/app.js"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git@codeup.aliyun.com:60b0836844816b8ed2335ed6/vweb/modules/mq-adapter.git"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@babel/cli": "^7.16.0",
|
|
20
|
+
"@babel/core": "^7.16.0",
|
|
21
|
+
"@babel/plugin-proposal-decorators": "^7.16.4",
|
|
22
|
+
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
|
23
|
+
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
24
|
+
"@babel/preset-env": "^7.16.4",
|
|
25
|
+
"@babel/register": "^7.16.0",
|
|
26
|
+
"@babel/runtime": "^7.16.3",
|
|
27
|
+
"@types/ali-oss": "^6.16.3",
|
|
28
|
+
"@types/node": "^16.11.12",
|
|
29
|
+
"ali-oss": "^6.18.1",
|
|
30
|
+
"axios": "^1.6.1",
|
|
31
|
+
"babel-plugin-transform-decorators-legacy": "^1.3.5",
|
|
32
|
+
"esdk-obs-nodejs": "^3.24.3",
|
|
33
|
+
"form-data": "^4.0.0",
|
|
34
|
+
"minio": "^8.0.0",
|
|
35
|
+
"vweb-core": "^3.0.14",
|
|
36
|
+
"vweb-mvc": "^1.2.18"
|
|
37
|
+
},
|
|
38
|
+
"author": "",
|
|
39
|
+
"license": "ISC",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"cos-nodejs-sdk-v5": "^2.14.4"
|
|
42
|
+
}
|
|
43
|
+
}
|