forked from lesonli/react-native-aliyun-oss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (89 loc) · 2.51 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
/**
* @flow
*/
'use strict';
import { NativeModules, NativeAppEventEmitter, NativeEventEmitter, Platform } from 'react-native';
const NativeAliyunOSS = NativeModules.AliyunOSS;
const UPLOAD_EVENT = 'uploadProgress';
const DOWNLOAD_EVENT = 'downloadProgress';
const _subscriptions = new Map();
const AliyunOSS = {
//开启oss log
enableOSSLog() {
NativeAliyunOSS.enableOSSLog();
},
/*初始化ossclient,
**通过AccessKey和SecretKey
*
*/
initWithKey(conf, EndPoint) {
NativeAliyunOSS.initWithKey(conf.AccessKey, conf.SecretKey, EndPoint);
},
/*初始化ossclient,
**通过签名字符串,此处采用的是服务端签名
*
*/
initWithSigner(AccessKey, Signature, EndPoint) {
NativeAliyunOSS.initWithSigner(AccessKey, Signature, EndPoint);
},
/*异步上传文件
**bucketName
*sourceFile:源文件路径,例如:/User/xx/xx/test.jpg
*ossFile:目标路径,例如:文件夹/文件名 test/test.jpg
*localPath:本地路径
*/
uploadObjectAsync(conf) {
return NativeAliyunOSS.uploadObjectAsync(conf.bucketName, conf.sourceFile, conf.ossFile);
},
downloadObjectAsync(conf) {
return NativeAliyunOSS.downloadObjectAsync(conf.bucketName, conf.ossFile, conf.localPath);
},
/*监听上传和下载事件,
**返回对象3个属性
*everySentSize:每次上传/下载字节
*currentSize:当前所需上传/下载字节
*totalSize:总字节
*/
addEventListener(type, handler) {
var listener;
if (Platform.OS === 'ios') {
const Emitter = new NativeEventEmitter(NativeAliyunOSS);
if (type === UPLOAD_EVENT) {
listener = Emitter.addListener('uploadProgress', uploadData => {
handler(uploadData);
});
} else if (type === DOWNLOAD_EVENT) {
listener = Emitter.addListener('downloadProgress', downloadData => {
handler(downloadData);
});
} else {
return false;
}
} else {
if (type === UPLOAD_EVENT) {
listener = NativeAppEventEmitter.addListener('uploadProgress', uploadData => {
handler(uploadData);
});
} else if (type === DOWNLOAD_EVENT) {
listener = NativeAppEventEmitter.addListener('downloadProgress', downloadData => {
handler(downloadData);
});
} else {
return false;
}
}
_subscriptions.set(handler, listener);
},
removeEventListener(type, handler) {
if (type !== UPLOAD_EVENT && type !== DOWNLOAD_EVENT) {
return false;
}
var listener = _subscriptions.get(handler);
if (!listener) {
return;
}
listener.remove();
_subscriptions.delete(handler);
},
};
module.exports = AliyunOSS;