oss-file-upload.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import request from "./request";
  2. import axios from "axios";
  3. // import umiRequest from "umi-request";
  4. import COS from "cos-js-sdk-v5";
  5. export const ossSwitch = "tencent"; //as 'ks3' | 'tencent'; // 上传文件服务商
  6. const tencentBucket = "daya-online-1303457149";
  7. /**
  8. * 管乐团 gyt/
  9. * 酷乐秀 klx/
  10. * 课堂乐器 ktqy/
  11. * 管乐迷 gym/
  12. */
  13. // 定义一个cos 对象
  14. /**
  15. * 获取上传文件签名
  16. * @param params 上传对应参数
  17. * { filename: fileName,
  18. bucketName: props.bucketName,
  19. postData: {
  20. filename: fileName,
  21. acl: 'public-read',
  22. key: fileName,
  23. unknowValueField: []
  24. }}
  25. * @param oss 服务商 ks3 tencent
  26. * @returns ”{'signatur'':'',''kssAccessKeyI'':'',''policy': '' }“
  27. */
  28. export const getUploadSign = async (params) => {
  29. const { bucketName, filename, postData } = params;
  30. const ossType = ossSwitch;
  31. let bucket = bucketName;
  32. let file = filename;
  33. // const key = postData.key;
  34. let tempPostData = {};
  35. if (ossType === "tencent") {
  36. bucket = tencentBucket;
  37. file = "gym/" + filename;
  38. tempPostData = {
  39. key: "gym/" + postData.key,
  40. };
  41. } else {
  42. tempPostData = postData;
  43. }
  44. return request.post("/getUploadSign?pluginName=" + ossType, {
  45. postData: tempPostData,
  46. pluginName: ossType,
  47. bucketName: bucket,
  48. filename: file,
  49. });
  50. };
  51. /**
  52. * 使用组件上传时,调用方法
  53. * @param param0
  54. */
  55. export const onFileUpload = ({ file, action, data, onProgress, onFinish, onError }) => {
  56. if (ossSwitch === "ks3") {
  57. const fileParams = {
  58. policy: data.policy,
  59. signature: data.signature,
  60. key: data.key,
  61. acl: "public-read",
  62. KSSAccessKeyId: data.KSSAccessKeyId,
  63. name: data.name,
  64. };
  65. const formData = new FormData();
  66. for (const key in fileParams) {
  67. formData.append(key, fileParams[key]);
  68. }
  69. formData.append("file", data.file);
  70. axios
  71. .post(action, formData, {
  72. onUploadProgress: ({ progress }) => {
  73. onProgress({ percent: Math.ceil((progress || 0) * 100) });
  74. },
  75. })
  76. .then(() => {
  77. file.url = action + data.key;
  78. onFinish();
  79. })
  80. .catch((error) => {
  81. onError(error);
  82. });
  83. } else {
  84. const cos = new COS({
  85. Domain: "https://oss.dayaedu.com",
  86. Protocol: "https",
  87. // getAuthorization 必选参数
  88. getAuthorization: async (options, callback) => {
  89. callback({ Authorization: data.signature });
  90. },
  91. });
  92. cos
  93. .uploadFile({
  94. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  95. Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
  96. Key: `gym/${data.name}`,
  97. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  98. Body: data.file.file, // 上传文件对象
  99. SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  100. onProgress: function (progressData) {
  101. onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  102. },
  103. })
  104. .then((res) => {
  105. // file.url = 'https://' + res.Location;
  106. if (res.Location?.indexOf("http") >= 0) {
  107. file.url = res.Location;
  108. } else {
  109. file.url = "https://" + res.Location;
  110. }
  111. onFinish();
  112. })
  113. .catch(() => {
  114. onError();
  115. });
  116. }
  117. };
  118. export const onOnlyFileUpload = async (action, params) => {
  119. if (ossSwitch === "ks3") {
  120. const fileParams = {
  121. policy: params.policy,
  122. signature: params.signature,
  123. key: params.key,
  124. acl: "public-read",
  125. KSSAccessKeyId: params.KSSAccessKeyId,
  126. name: params.name,
  127. };
  128. const formData = new FormData();
  129. for (const key in fileParams) {
  130. formData.append(key, fileParams[key]);
  131. }
  132. formData.append("file", params.file);
  133. let file = "";
  134. let errorObj = null;
  135. await axios
  136. .post(action, formData, {
  137. // onUploadProgress: ({ progress }) => {
  138. // console.log(progress);
  139. // onProgress({ percent: Math.ceil((progress || 0) * 100) });
  140. // }
  141. })
  142. .then(() => {
  143. file = action + params.key;
  144. })
  145. .catch((error) => {
  146. // onError(error);
  147. errorObj = error;
  148. // throw new Error(error);
  149. });
  150. if (file) {
  151. return file;
  152. } else {
  153. throw new Error(errorObj);
  154. }
  155. } else {
  156. let file = "";
  157. let errorObj = null;
  158. // console.log(params, "params");
  159. const cos = new COS({
  160. Domain: "https://oss.dayaedu.com",
  161. // getAuthorization 必选参数
  162. getAuthorization: async (options, callback) => {
  163. callback({ Authorization: params.signature });
  164. },
  165. });
  166. await cos
  167. .uploadFile({
  168. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  169. Region: "ap-nanjing" /* 存储桶所在地域,必须字段 */,
  170. Key: `gym/${params.name}`,
  171. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  172. Body: params.file, // 上传文件对象
  173. SliceSize: 1024 * 1024 * 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  174. // onProgress: function (progressData) {
  175. // onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  176. // }
  177. })
  178. .then((res) => {
  179. // file.url = 'https://' + res.Location;
  180. // file = 'https://' + res.Location;
  181. if (res.Location?.indexOf("http") >= 0) {
  182. file = res.Location;
  183. } else {
  184. file = "https://" + res.Location;
  185. }
  186. // onFinish();
  187. })
  188. .catch((error) => {
  189. // console.log(error, 'error');
  190. // onError();
  191. // throw new Error(error);
  192. errorObj = error;
  193. });
  194. if (file) {
  195. return file;
  196. } else {
  197. throw new Error(errorObj);
  198. }
  199. }
  200. };