aliyun-oss-react-native

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import {DeviceEventEmitter, NativeEventEmitter, NativeModules, Platform} from "react-native";
  2. const {RNAliyunOSS} = NativeModules;
  3. let subscription;
  4. //default configuration for OSS Client
  5. const conf = {
  6. maxRetryCount: 3,
  7. timeoutIntervalForRequest: 30,
  8. timeoutIntervalForResource: 24 * 60 * 60
  9. };
  10. const imageXOssProcess = {
  11. "x-oss-process":''
  12. }
  13. let partSize = 128 * 1024
  14. const mulitpartUploadConfig = {
  15. "partSize":partSize
  16. }
  17. //appendObject
  18. const appendOptions = {
  19. "appendPosition":0,
  20. "contentType":'',
  21. "contentMd5":'',
  22. "contentEncoding":'',
  23. "contentDisposition":''
  24. }
  25. export default AliyunOSS = {
  26. //Enable dev mode
  27. enableDevMode() {
  28. RNAliyunOSS.enableDevMode();
  29. },
  30. /**
  31. * Initialize the OSS Client
  32. * Mode: PlainTextAKSK
  33. */
  34. initWithPlainTextAccessKey(accessKey, secretKey, endPoint, configuration = conf) {
  35. RNAliyunOSS.initWithPlainTextAccessKey(accessKey, secretKey, endPoint, configuration);
  36. },
  37. /**
  38. * Initialize the OSS Client
  39. * Mode: ImplementedSigner
  40. */
  41. initWithImplementedSigner(signature, accessKey, endPoint, configuration = conf) {
  42. RNAliyunOSS.initWithImplementedSigner(signature, accessKey, endPoint, configuration);
  43. },
  44. /**
  45. * Initialize the OSS Client
  46. * Mode: SecurityToken (STS)
  47. */
  48. initWithSecurityToken(securityToken, accessKey, secretKey, endPoint, configuration = conf) {
  49. RNAliyunOSS.initWithSecurityToken(securityToken, accessKey, secretKey, endPoint, configuration);
  50. },
  51. /**
  52. * Initialize the OSS Client
  53. * Server STS
  54. */
  55. initWithServerSTS(server, endPoint, configuration = conf) {
  56. RNAliyunOSS.initWithServerSTS(server, endPoint, configuration);
  57. },
  58. /**
  59. * Asynchronously uploading
  60. */
  61. asyncUpload(bucketName, objectKey, filepath,options) {
  62. return RNAliyunOSS.asyncUpload(bucketName, objectKey, filepath,options);
  63. },
  64. /**
  65. * Asynchronously
  66. */
  67. asyncResumableUpload(bucketName, objectKey, filepath='',options={}) {
  68. return RNAliyunOSS.asyncResumableUpload(bucketName, objectKey, filepath,options);
  69. },
  70. /**
  71. * Asynchronously asyncAppendObject
  72. */
  73. asyncAppendObject(bucketName,objectKey,filepath,options = appendOptions) {
  74. return RNAliyunOSS.asyncAppendObject(bucketName, objectKey, filepath,options);
  75. },
  76. /**
  77. * Asynchronously
  78. */
  79. initMultipartUpload(bucketName,objectKey) {
  80. return RNAliyunOSS.initMultipartUpload(bucketName, objectKey);
  81. },
  82. /**
  83. * Asynchronously multipartUpload
  84. */
  85. multipartUpload(bucketName,objectKey,uploadId,filepath ='',options = mulitpartUploadConfig) {
  86. return RNAliyunOSS.multipartUpload(bucketName, objectKey, uploadId,filepath, options);
  87. },
  88. /**
  89. * Asynchronously listParts
  90. */
  91. listParts (bucketName,objectKey,uploadId) {
  92. return RNAliyunOSS.listParts(bucketName, objectKey, uploadId)
  93. },
  94. /**
  95. * Asynchronously abortMultipartUpload
  96. */
  97. abortMultipartUpload(bucketName,objectKey,uploadId) {
  98. return RNAliyunOSS.abortMultipartUpload(bucketName, objectKey, uploadId);
  99. },
  100. /**
  101. * Asynchronously downloading
  102. */
  103. asyncDownload(bucketName, objectKey, filepath='',options = imageXOssProcess) {
  104. return RNAliyunOSS.asyncDownload(bucketName, objectKey, filepath,options);
  105. },
  106. /*
  107. asyncListBuckets
  108. */
  109. asyncListBuckets () {
  110. return RNAliyunOSS.asyncListBuckets()
  111. },
  112. /**
  113. * Asynchronously getHeadObject
  114. */
  115. asyncHeadObject (bucketName, objectKey) {
  116. return RNAliyunOSS.asyncHeadObject(bucketName,objectKey)
  117. },
  118. /**
  119. * Asynchronously getAsyncObjects
  120. */
  121. asyncListObjects (bucketName,prefix='') {
  122. return RNAliyunOSS.asyncListObjects(bucketName,prefix,)
  123. },
  124. /**
  125. * Asynchronously asyncCopyObject
  126. */
  127. asyncCopyObject (srcBucketName, srcObjectKey, desBucketName,destObjectKey, options) {
  128. return RNAliyunOSS.asyncCopyObject (srcBucketName, srcObjectKey, desBucketName,destObjectKey, options)
  129. },
  130. /**
  131. * Asynchronously doesObjectExist
  132. */
  133. doesObjectExist (bucketName, objectKey) {
  134. return RNAliyunOSS.doesObjectExist(bucketName, objectKey)
  135. },
  136. /**
  137. * Asynchronously asyncDeleteObject
  138. */
  139. asyncDeleteObject (bucketName, objectKey) {
  140. return RNAliyunOSS.asyncDeleteObject(bucketName, objectKey)
  141. },
  142. /**
  143. * Asynchronously createBucket
  144. */
  145. asyncCreateBucket (bucketName,acl="private",region) {
  146. return RNAliyunOSS.asyncCreateBucket(bucketName,acl,region)
  147. },
  148. /**
  149. * Asynchronously getBucketACL
  150. */
  151. asyncGetBucketACL (bucketName) {
  152. return RNAliyunOSS.asyncGetBucketACL(bucketName)
  153. },
  154. /**
  155. * Asynchronously getBucketACL
  156. */
  157. asyncGetBucketACL(bucketName) {
  158. return RNAliyunOSS.asyncGetBucketACL(bucketName)
  159. },
  160. /**
  161. * Asynchronously deleteBucket
  162. */
  163. asyncDeleteBucket (bucketName) {
  164. return RNAliyunOSS.asyncDeleteBucket(bucketName)
  165. },
  166. /**
  167. * event listener for native upload/download event
  168. * @param event one of 'uploadProgress' or 'downloadProgress'
  169. * @param callback a callback function accepts one params: event
  170. */
  171. addEventListener(event, callback) {
  172. const RNAliyunEmitter = Platform.OS === 'ios' ? new NativeEventEmitter(RNAliyunOSS) : DeviceEventEmitter;
  173. switch (event) {
  174. case 'uploadProgress':
  175. subscription = RNAliyunEmitter.addListener(
  176. 'uploadProgress',
  177. e => callback(e)
  178. );
  179. break;
  180. case 'downloadProgress':
  181. subscription = RNAliyunEmitter.addListener(
  182. 'downloadProgress',
  183. e => callback(e)
  184. );
  185. break;
  186. default:
  187. break;
  188. }
  189. },
  190. /**
  191. * remove event listener for native upload/download event
  192. * @param event one of 'uploadProgress' or 'downloadProgress'
  193. */
  194. removeEventListener(event) {
  195. switch (event) {
  196. case 'uploadProgress':
  197. subscription.remove();
  198. break;
  199. case 'downloadProgress':
  200. subscription.remove();
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. };