demo-sdk.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. var COS = require('./lib/cos-wx-sdk-v5');
  2. var wxfs = wx.getFileSystemManager();
  3. var config = require('./config');
  4. var TaskId;
  5. // 签名回调
  6. var getAuthorization = function(options, callback) {
  7. // 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
  8. // 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
  9. // 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
  10. wx.request({
  11. method: 'GET',
  12. url: config.stsUrl, // 服务端签名,参考 server 目录下的两个签名例子
  13. dataType: 'json',
  14. success: function(result) {
  15. var data = result.data;
  16. var credentials = data && data.credentials;
  17. if (!data || !credentials) return console.error('credentials invalid');
  18. callback({
  19. TmpSecretId: credentials.tmpSecretId,
  20. TmpSecretKey: credentials.tmpSecretKey,
  21. SecurityToken: credentials.sessionToken,
  22. StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  23. ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
  24. });
  25. }
  26. });
  27. // // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重复使用临时密钥,后端可以通过 Scope 细粒度控制权限
  28. // // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
  29. // wx.request({
  30. // method: 'POST',
  31. // url: 'http://127.0.0.1:3000/sts-scope',
  32. // data: options.Scope,
  33. // dataType: 'json',
  34. // success: function(result) {
  35. // var data = result.data;
  36. // var credentials = data && data.credentials;
  37. // if (!data || !credentials) return console.error('credentials invalid');
  38. // callback({
  39. // TmpSecretId: credentials.tmpSecretId,
  40. // TmpSecretKey: credentials.tmpSecretKey,
  41. // XCosSecurityToken: credentials.sessionToken,
  42. // StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  43. // ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
  44. // ScopeLimit: true, // 细粒度控制权限需要设为 true,会限制密钥只在相同请求时重复使用
  45. // });
  46. // }
  47. // });
  48. // // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
  49. // // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
  50. // // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
  51. // wx.request({
  52. // method: 'POST',
  53. // url: 'https://example.com/sts-auth.php, // 服务端签名,参考 server 目录下的两个签名例子
  54. // data: {
  55. // method: options.Method,
  56. // pathname: options.Pathname,
  57. // query: options.Query,
  58. // headers: options.Headers,
  59. // },
  60. // dataType: 'json',
  61. // success: function(result) {
  62. // var data = result.data;
  63. // if (!data || !data.authorization) return console.error('authorization invalid');
  64. // callback({
  65. // Authorization: data.authorization,
  66. // // XCosSecurityToken: data.sessionToken, // 如果使用临时密钥,需要传 sessionToken
  67. // });
  68. // }
  69. // });
  70. // // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名
  71. // var authorization = COS.getAuthorization({
  72. // SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  73. // SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  74. // Method: options.Method,
  75. // Pathname: options.Pathname,
  76. // Query: options.Query,
  77. // Headers: options.Headers,
  78. // Expires: 60,
  79. // });
  80. // callback({
  81. // Authorization: authorization,
  82. // // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
  83. // });
  84. };
  85. var cos = new COS({
  86. // path style 指正式请求时,Bucket 是在 path 里,这样用相同园区多个 bucket 只需要配置一个园区域名
  87. // ForcePathStyle: true,
  88. getAuthorization: getAuthorization,
  89. // 是否使用全球加速域名。开启该配置后仅以下接口支持操作:putObject、getObject、headObject、optionsObject、multipartInit、multipartListPart、multipartUpload、multipartAbort、multipartComplete、multipartList、sliceUploadFile、uploadFiles
  90. // UseAccelerate: true,
  91. });
  92. // 回调统一处理函数
  93. var requestCallback = function(err, data) {
  94. console.log(err || data);
  95. if (err && err.error) {
  96. wx.showModal({
  97. title: '返回错误',
  98. content: '请求失败:' + (err.error.Message || err.error) + ';状态码:' + err.statusCode,
  99. showCancel: false
  100. });
  101. } else if (err) {
  102. wx.showModal({
  103. title: '请求出错',
  104. content: '请求出错:' + err + ';状态码:' + err.statusCode,
  105. showCancel: false
  106. });
  107. } else {
  108. wx.showToast({
  109. title: '请求成功',
  110. icon: 'success',
  111. duration: 3000
  112. });
  113. }
  114. };
  115. var mylog = function (msg) {
  116. wx.showToast({
  117. title: msg,
  118. icon: 'success',
  119. duration: 3000
  120. });
  121. };
  122. var toolsDao = {
  123. 'request': function(){
  124. // 对云上数据进行图片处理
  125. cos.request({
  126. Bucket: config.Bucket,
  127. Region: config.Region,
  128. Key: 'photo.png',
  129. Method: 'POST',
  130. Action: 'image_process',
  131. Headers: {
  132. // 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩
  133. 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}'
  134. },
  135. }, (err, data) => {
  136. console.log(err || data)
  137. });
  138. },
  139. 'uploadFile 高级上传': function() {
  140. var uploadFile = function(file) {
  141. cos.uploadFile({
  142. Bucket: config.Bucket,
  143. Region: config.Region,
  144. Key: file.name,
  145. FilePath: file.path,
  146. FileSize: file.size,
  147. SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传
  148. onTaskReady: function(taskId) {
  149. TaskId = taskId
  150. },
  151. onProgress: function (info) {
  152. var percent = parseInt(info.percent * 10000) / 100;
  153. var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
  154. console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
  155. },
  156. onFileFinish: function (err, data, options) {
  157. console.log(options.Key + '上传' + (err ? '失败' : '完成'));
  158. },
  159. }, function (err, data) {
  160. console.log(err || data);
  161. });
  162. }
  163. wx.chooseMessageFile({
  164. count: 10,
  165. type: 'all',
  166. success: function(res) {
  167. uploadFile(res.tempFiles[0]);
  168. }
  169. });
  170. },
  171. 'uploadFiles 批量上传': function() {
  172. var uploadFiles = function(files) {
  173. const fileList = files.map(function(file) {
  174. return Object.assign(file, {
  175. Bucket: config.Bucket,
  176. Region: config.Region,
  177. Key: file.name,
  178. FilePath: file.path,
  179. });
  180. });
  181. cos.uploadFiles({
  182. files: fileList,
  183. SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传
  184. onProgress: function (info) {
  185. var percent = parseInt(info.percent * 10000) / 100;
  186. var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
  187. console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
  188. },
  189. onFileFinish: function (err, data, options) {
  190. console.log(options.Key + '上传' + (err ? '失败' : '完成'));
  191. },
  192. }, function (err, data) {
  193. console.log(err || data);
  194. });
  195. }
  196. wx.chooseMessageFile({
  197. count: 10,
  198. type: 'all',
  199. success: function(res) {
  200. uploadFiles(res.tempFiles);
  201. }
  202. });
  203. },
  204. 'sliceUploadFile 分片上传': function() {
  205. var sliceUploadFile = function (file) {
  206. var key = file.name;
  207. cos.sliceUploadFile({
  208. Bucket: config.Bucket,
  209. Region: config.Region,
  210. Key: key,
  211. FilePath: file.path,
  212. FileSize: file.size,
  213. CacheControl: 'max-age=7200',
  214. Headers: {
  215. aa: 123,
  216. },
  217. Query: {
  218. bb: 123,
  219. },
  220. onTaskReady: function(taskId) {
  221. TaskId = taskId
  222. },
  223. onHashProgress: function(info) {
  224. console.log('check hash', JSON.stringify(info));
  225. },
  226. onProgress: function(info) {
  227. console.log(JSON.stringify(info));
  228. }
  229. }, requestCallback);
  230. };
  231. wx.chooseMessageFile({
  232. count: 10,
  233. type: 'all',
  234. success: function(res) {
  235. sliceUploadFile(res.tempFiles[0]);
  236. }
  237. });
  238. // wx.chooseVideo({
  239. // sourceType: ['album','camera'],
  240. // maxDuration: 60,
  241. // camera: 'back',
  242. // success(res) {
  243. // var name = res.tempFilePath.replace(/^.*?([^/]{32}\.\w+)$/, '$1');
  244. // sliceUploadFile({
  245. // name: name,
  246. // path: res.tempFilePath,
  247. // size: res.size,
  248. // });
  249. // },
  250. // fail(err) {
  251. // console.log(err);
  252. // }
  253. // })
  254. },
  255. // 上传文件适用于单请求上传大文件
  256. 'postObject 简单上传': function() {
  257. wx.chooseImage({
  258. count: 1, // 默认9
  259. sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
  260. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  261. success: function(res) {
  262. var file = res.tempFiles[0];
  263. cos.postObject({
  264. Bucket: config.Bucket,
  265. Region: config.Region,
  266. Key: '1.png',
  267. FilePath: file.path,
  268. onTaskReady: function(taskId) {
  269. TaskId = taskId
  270. },
  271. onProgress: function(info) {
  272. console.log(JSON.stringify(info));
  273. }
  274. }, requestCallback);
  275. }
  276. })
  277. },
  278. 'putObject 简单上传文件': function(type) {
  279. wx.chooseMessageFile({
  280. count: 10,
  281. type: 'all',
  282. success: function(res) {
  283. var file = res.tempFiles[0];
  284. wxfs.readFile({
  285. filePath: file.path,
  286. success: function (res) {
  287. cos.putObject({
  288. Bucket: config.Bucket,
  289. Region: config.Region,
  290. Key: file.name,
  291. Body: res.data, // 在小程序里,putObject 接口只允许传字符串的内容,不支持 TaskReady 和 onProgress,上传请使用 cos.postObject 接口
  292. Headers: {
  293. // 万象持久化接口,上传时持久化。例子:通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩
  294. // 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}'
  295. },
  296. }, requestCallback);
  297. },
  298. fail: err => console.error(err),
  299. });
  300. },
  301. fail: err => console.error(err),
  302. });
  303. },
  304. 'putObject 上传字符串': function(type) {
  305. cos.putObject({
  306. Bucket: config.Bucket,
  307. Region: config.Region,
  308. Key: '1.txt',
  309. Body: 'hello world', // 在小程序里,putObject 接口只允许传字符串的内容,不支持 TaskReady 和 onProgress,上传请使用 cos.postObject 接口
  310. Headers: {
  311. aa: 123,
  312. },
  313. Query: {
  314. bb: 123,
  315. },
  316. }, requestCallback);
  317. },
  318. // 上传文件
  319. 'putObject base64 转 ArrayBuffer 上传': function() {
  320. var base64Url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAABRFBMVEUAAAAAo/8Ao/8Ao/8Ao/8ApP8Aov8Ao/8Abv8Abv8AyNwAyNwAo/8Ao/8Ao/8Abv8Ao/8AivgAo/8AyNwAbv8Abv8AydwApf8Abf8Ao/8AbP8Ao/8AyNwAydwAbv8AydwApP8Ao/8AyNwAo/8AyNwAydsAyNwAxd8Aov8AyNwAytsAo/8Abv8AyNwAbv8Av+MAo/8AytsAo/8Abv8AyNwAo/8Abv8AqfkAbv8Aov8Abv8AyNwAov8Abv8Ao/8Abv8Ao/8AydwAo/8Ao/8Ate8Ay9oAvOcAof8AveAAyNwAyNwAo/8AyNwAy9kAo/8AyNwAyNwAo/8AqP8Aaf8AyNwAbv0Abv8Abv8AaP8Ao/8Ao/8Ao/8Ao/8Abv8AyNwAgvcAaP8A0dkAo/8AyNwAav8Abv8Ao/8Abv8AyNwAy9sAvOUAtePdkYxjAAAAZnRSTlMAw/co8uAuJAn8+/Tt29R8DAX77+nZz87Jv6CTh3lxTklAPjouJRsL5tjAuLiyr62roaCakYp0XVtOQTMyLiohICAcGRP49vTv5+PJurawq6mnnJuYl4+OiIB7eXVvX15QSDgqHxNcw3l6AAABe0lEQVQ4y82P11oCQQxGIy5FUJpKk6aAhV6k92LvvXedDfj+92ZkYQHxnnMxu3/OfJMEJo6y++baXf5XVw22GVGcsRmq431mQZRYyIzRGgdXi+HwIv86NDBKisrRAtU1hSj9pkZ9jpo/9YKbRsmNNKCHDXI00BxfMMirKNpMcjQ5Lm4/YZArUXyBYUwg40nsdr5jb3LBe25VWpNeKa1GENsEnq52C80z1uW48estiKjb19G54QdCrScnKAU69U3KJ4jzrsBawDWPuOcBqMyRvlcb1Y+zjMUBVsivAKe4gXgEKiVjSh9wlunGMmwiOqFL3RI0cj+nkgp3jC1BELVFkGiZSuvkp3tZZWZ2sKCuDj185PXqfmwI7AAOUctHkJoOeXg3sxA4ES+l7CVvrYHMEmNp8GtR+wycPG0+1RrwWQUzl4CvgQmPP5Ddofl8tWkJVT7J+BIAaxEktrYZoRAUfXgOGYHfcOqw3WF/EdLccz5cMfvUCPb4QwUmhB8+v12HZPCkbgAAAABJRU5ErkJggg==';
  321. var m = (/data:image\/(\w+);base64,(.*)/.exec(base64Url) || []);
  322. var format = m[1];
  323. var bodyData = m[2];
  324. var fileBuf = wx.base64ToArrayBuffer(bodyData);
  325. cos.putObject({
  326. Bucket: config.Bucket,
  327. Region: config.Region,
  328. Key: '1.' + format,
  329. Body: fileBuf,
  330. }, requestCallback);
  331. },
  332. 'getObjectUrl 获取对象访问url': function() {
  333. var url = cos.getObjectUrl({
  334. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  335. Region: config.Region,
  336. Key: '1mb.zip',
  337. Expires: 60,
  338. Sign: true,
  339. }, function(err, data) {
  340. console.log(err || data);
  341. });
  342. console.log(url);
  343. },
  344. };
  345. var bucketDao = {
  346. // Service
  347. 'getService 获取存储桶列表': function() {
  348. cos.getService(requestCallback);
  349. },
  350. // 简单 Bucket 操作
  351. 'putBucket 创建存储桶': function() {
  352. cos.putBucket({
  353. Bucket: config.Bucket,
  354. Region: config.Region,
  355. }, requestCallback);
  356. },
  357. 'headBucket 检索存储桶及其权限': function() {
  358. cos.headBucket({
  359. Bucket: config.Bucket,
  360. Region: config.Region
  361. }, requestCallback);
  362. },
  363. 'deleteBucket 删除存储桶': function() {
  364. cos.deleteBucket({
  365. Bucket: config.Bucket,
  366. Region: config.Region
  367. }, requestCallback);
  368. },
  369. 'getBucketACL 查询存储桶 ACL': function() {
  370. cos.getBucketAcl({
  371. Bucket: config.Bucket,
  372. Region: config.Region
  373. }, requestCallback);
  374. },
  375. 'putBucketACL 设置存储桶 ACL': function() {
  376. cos.putBucketAcl({
  377. Bucket: config.Bucket,
  378. Region: config.Region,
  379. ACL: 'public-read'
  380. }, requestCallback);
  381. },
  382. 'getBucketCors 查询跨域配置': function() {
  383. cos.getBucketCors({
  384. Bucket: config.Bucket,
  385. Region: config.Region
  386. }, requestCallback);
  387. },
  388. 'putBucketCors 设置跨域配置': function() {
  389. cos.putBucketCors({
  390. Bucket: config.Bucket,
  391. Region: config.Region,
  392. ResponseVary: "true",
  393. CORSRules: [{
  394. "AllowedOrigin": ["*"],
  395. "AllowedMethod": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  396. "AllowedHeader": ["*"],
  397. "ExposeHeader": ["ETag", "Content-Length"],
  398. "MaxAgeSeconds": "5"
  399. }]
  400. }, requestCallback);
  401. },
  402. 'deleteBucketCors 删除跨域配置': function() {
  403. cos.deleteBucketCors({
  404. Bucket: config.Bucket,
  405. Region: config.Region
  406. }, requestCallback);
  407. },
  408. 'putBucketPolicy 设置存储桶策略': function() {
  409. var AppId = config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1);
  410. cos.putBucketPolicy({
  411. Bucket: config.Bucket,
  412. Region: config.Region,
  413. Policy: {
  414. "version": "2.0",
  415. "principal": {
  416. "qcs": ["qcs::cam::uin/10001:uin/10001"]
  417. }, // 这里的 10001 是 QQ 号
  418. "statement": [{
  419. "effect": "allow",
  420. "action": [
  421. "name/cos:GetBucket",
  422. "name/cos:PutObject",
  423. "name/cos:PostObject",
  424. "name/cos:PutObjectCopy",
  425. "name/cos:InitiateMultipartUpload",
  426. "name/cos:UploadPart",
  427. "name/cos:UploadPartCopy",
  428. "name/cos:CompleteMultipartUpload",
  429. "name/cos:AbortMultipartUpload",
  430. "name/cos:AppendObject"
  431. ],
  432. // "resource": ["qcs::cos:ap-guangzhou:uid/1250000000:test-1250000000/*"] // 1250000000 是 appid
  433. "resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + config.Bucket + "/*"] // 1250000000 是 appid
  434. }]
  435. },
  436. }, requestCallback);
  437. },
  438. 'getBucketPolicy 查询存储桶策略': function() {
  439. cos.getBucketPolicy({
  440. Bucket: config.Bucket,
  441. Region: config.Region
  442. }, requestCallback);
  443. },
  444. 'deleteBucketPolicy 删除存储桶策略': function() {
  445. cos.deleteBucketPolicy({
  446. Bucket: config.Bucket,
  447. Region: config.Region
  448. }, requestCallback);
  449. },
  450. 'getBucketLocation 获取Bucket的地域信息': function() {
  451. cos.getBucketLocation({
  452. Bucket: config.Bucket,
  453. Region: config.Region
  454. }, requestCallback);
  455. },
  456. 'getBucketTagging 获取Bucket标签': function() {
  457. cos.getBucketTagging({
  458. Bucket: config.Bucket,
  459. Region: config.Region
  460. }, requestCallback);
  461. },
  462. 'putBucketTagging 设置Bucket标签': function() {
  463. cos.putBucketTagging({
  464. Bucket: config.Bucket,
  465. Region: config.Region,
  466. Tagging: {
  467. "Tags": [
  468. {
  469. "Key": "k1",
  470. "Value": "v1"
  471. },
  472. {
  473. "Key": "k2",
  474. "Value": "v2"
  475. }
  476. ]
  477. }
  478. }, requestCallback);
  479. },
  480. 'deleteBucketTagging 删除存储桶标签': function() {
  481. cos.deleteBucketTagging({
  482. Bucket: config.Bucket,
  483. Region: config.Region
  484. }, requestCallback);
  485. },
  486. };
  487. var objectDao = {
  488. 'getBucket 获取对象列表': function() {
  489. cos.getBucket({
  490. Bucket: config.Bucket,
  491. Region: config.Region
  492. }, requestCallback);
  493. },
  494. // 上传文件适用于单请求上传大文件
  495. 'postObject 表单上传对象': function() {
  496. wx.chooseImage({
  497. count: 1, // 默认9
  498. sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
  499. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  500. success: function(res) {
  501. var file = res.tempFiles[0];
  502. cos.postObject({
  503. Bucket: config.Bucket,
  504. Region: config.Region,
  505. Key: '1.png',
  506. FilePath: file.path,
  507. onTaskReady: function(taskId) {
  508. TaskId = taskId
  509. },
  510. onProgress: function(info) {
  511. console.log(JSON.stringify(info));
  512. }
  513. }, requestCallback);
  514. }
  515. })
  516. },
  517. 'putObject 简单上传文件': function(type) {
  518. wx.chooseMessageFile({
  519. count: 10,
  520. type: 'all',
  521. success: function(res) {
  522. var file = res.tempFiles[0];
  523. wxfs.readFile({
  524. filePath: file.path,
  525. success: function (res) {
  526. cos.putObject({
  527. Bucket: config.Bucket,
  528. Region: config.Region,
  529. Key: file.name,
  530. Body: res.data, // 在小程序里,putObject 接口只允许传字符串的内容,不支持 TaskReady 和 onProgress,上传请使用 cos.postObject 接口
  531. Headers: {
  532. // 万象持久化接口,上传时持久化。例子:通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩
  533. // 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}'
  534. },
  535. }, requestCallback);
  536. },
  537. fail: err => console.error(err),
  538. });
  539. },
  540. fail: err => console.error(err),
  541. });
  542. },
  543. 'putObject 上传字符串': function(type) {
  544. cos.putObject({
  545. Bucket: config.Bucket,
  546. Region: config.Region,
  547. Key: '1.txt',
  548. Body: 'hello world', // 在小程序里,putObject 接口只允许传字符串的内容,不支持 TaskReady 和 onProgress,上传请使用 cos.postObject 接口
  549. Headers: {
  550. aa: 123,
  551. },
  552. Query: {
  553. bb: 123,
  554. },
  555. }, requestCallback);
  556. },
  557. // 上传文件
  558. 'putObject base64 转 ArrayBuffer 上传': function() {
  559. var base64Url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAABRFBMVEUAAAAAo/8Ao/8Ao/8Ao/8ApP8Aov8Ao/8Abv8Abv8AyNwAyNwAo/8Ao/8Ao/8Abv8Ao/8AivgAo/8AyNwAbv8Abv8AydwApf8Abf8Ao/8AbP8Ao/8AyNwAydwAbv8AydwApP8Ao/8AyNwAo/8AyNwAydsAyNwAxd8Aov8AyNwAytsAo/8Abv8AyNwAbv8Av+MAo/8AytsAo/8Abv8AyNwAo/8Abv8AqfkAbv8Aov8Abv8AyNwAov8Abv8Ao/8Abv8Ao/8AydwAo/8Ao/8Ate8Ay9oAvOcAof8AveAAyNwAyNwAo/8AyNwAy9kAo/8AyNwAyNwAo/8AqP8Aaf8AyNwAbv0Abv8Abv8AaP8Ao/8Ao/8Ao/8Ao/8Abv8AyNwAgvcAaP8A0dkAo/8AyNwAav8Abv8Ao/8Abv8AyNwAy9sAvOUAtePdkYxjAAAAZnRSTlMAw/co8uAuJAn8+/Tt29R8DAX77+nZz87Jv6CTh3lxTklAPjouJRsL5tjAuLiyr62roaCakYp0XVtOQTMyLiohICAcGRP49vTv5+PJurawq6mnnJuYl4+OiIB7eXVvX15QSDgqHxNcw3l6AAABe0lEQVQ4y82P11oCQQxGIy5FUJpKk6aAhV6k92LvvXedDfj+92ZkYQHxnnMxu3/OfJMEJo6y++baXf5XVw22GVGcsRmq431mQZRYyIzRGgdXi+HwIv86NDBKisrRAtU1hSj9pkZ9jpo/9YKbRsmNNKCHDXI00BxfMMirKNpMcjQ5Lm4/YZArUXyBYUwg40nsdr5jb3LBe25VWpNeKa1GENsEnq52C80z1uW48estiKjb19G54QdCrScnKAU69U3KJ4jzrsBawDWPuOcBqMyRvlcb1Y+zjMUBVsivAKe4gXgEKiVjSh9wlunGMmwiOqFL3RI0cj+nkgp3jC1BELVFkGiZSuvkp3tZZWZ2sKCuDj185PXqfmwI7AAOUctHkJoOeXg3sxA4ES+l7CVvrYHMEmNp8GtR+wycPG0+1RrwWQUzl4CvgQmPP5Ddofl8tWkJVT7J+BIAaxEktrYZoRAUfXgOGYHfcOqw3WF/EdLccz5cMfvUCPb4QwUmhB8+v12HZPCkbgAAAABJRU5ErkJggg==';
  560. var m = (/data:image\/(\w+);base64,(.*)/.exec(base64Url) || []);
  561. var format = m[1];
  562. var bodyData = m[2];
  563. var fileBuf = wx.base64ToArrayBuffer(bodyData);
  564. cos.putObject({
  565. Bucket: config.Bucket,
  566. Region: config.Region,
  567. Key: '1.' + format,
  568. Body: fileBuf,
  569. }, requestCallback);
  570. },
  571. 'getObject 下载对象': function() {
  572. cos.getObject({
  573. Bucket: config.Bucket,
  574. Region: config.Region,
  575. Key: '1.png',
  576. // 下载时使用图片处理
  577. // QueryString: `imageMogr2/thumbnail/200x/`,
  578. }, requestCallback);
  579. },
  580. 'headObject 检索对象': function() {
  581. cos.headObject({
  582. Bucket: config.Bucket,
  583. Region: config.Region,
  584. Key: '1.txt'
  585. }, requestCallback);
  586. },
  587. 'deleteObject 删除对象': function() {
  588. cos.deleteObject({
  589. Bucket: config.Bucket,
  590. Region: config.Region,
  591. Key: '1.txt'
  592. }, requestCallback);
  593. },
  594. 'getObjectACL 获取对象ACL': function() {
  595. cos.getObjectAcl({
  596. Bucket: config.Bucket,
  597. Region: config.Region,
  598. Key: '1.txt'
  599. }, requestCallback);
  600. },
  601. 'putObjectACL 设置对象ACL': function() {
  602. cos.putObjectAcl({
  603. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  604. Region: config.Region,
  605. Key: '1.txt',
  606. // GrantFullControl: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
  607. // GrantWrite: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
  608. // GrantRead: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
  609. // ACL: 'public-read-write',
  610. // ACL: 'public-read',
  611. // ACL: 'private',
  612. ACL: 'default', // 继承上一级目录权限
  613. // AccessControlPolicy: {
  614. // "Owner": { // AccessControlPolicy 里必须有 owner
  615. // "ID": 'qcs::cam::uin/459000000:uin/459000000' // 459000000 是 Bucket 所属用户的 QQ 号
  616. // },
  617. // "Grants": [{
  618. // "Grantee": {
  619. // "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  620. // },
  621. // "Permission": "READ"
  622. // }]
  623. // }
  624. }, requestCallback);
  625. },
  626. 'deleteMultipleObject 批量删除对象': function() {
  627. cos.deleteMultipleObject({
  628. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  629. Region: config.Region,
  630. Objects: [{
  631. Key: '1.txt'
  632. },
  633. {
  634. Key: '1.copy.txt'
  635. },
  636. ]
  637. }, requestCallback);
  638. },
  639. 'putObjectCopy 复制对象': function() {
  640. cos.putObjectCopy({
  641. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  642. Region: config.Region,
  643. Key: '1.copy.txt',
  644. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/1.txt',
  645. }, requestCallback);
  646. },
  647. 'restoreObject 恢复归档对象': function() {
  648. cos.restoreObject({
  649. Bucket: config.Bucket,
  650. Region: config.Region,
  651. Key: '1.txt',
  652. RestoreRequest: {
  653. Days: 1,
  654. CASJobParameters: {
  655. Tier: 'Expedited'
  656. }
  657. }
  658. }, requestCallback);
  659. },
  660. 'appendObject 追加上传': function() {
  661. // 初始化一个可追加上传的对象append.txt
  662. cos.appendObject({
  663. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  664. Region: config.Region,
  665. Key: 'append.txt', /* 必须 */
  666. Body: '12345',
  667. Position: 0,
  668. },
  669. function(err, data) {
  670. console.log('putObject:', err || data);
  671. })
  672. },
  673. 'appendObject_continue 继续追加上传': function() {
  674. // append.txt必须是一个可追加上传的对象,参考文档:https://cloud.tencent.com/document/product/436/7741
  675. cos.headObject({
  676. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  677. Region: config.Region,
  678. Key: 'append.txt', /* 必须 */
  679. }, function(err, data) {
  680. if (err) return console.log(err);
  681. // 首先取到要追加的文件当前长度,即需要上送的Position
  682. var position = data.headers['content-length'];
  683. cos.appendObject({
  684. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  685. Region: config.Region,
  686. Key: 'append.txt', /* 必须 */
  687. Body: '66666',
  688. Position: position,
  689. },
  690. function(err, data) {
  691. // 也可以取到下一次上传的position继续追加上传
  692. // var nextPosition = data.headers['x-cos-next-append-position'];
  693. console.log('putObject:', err || data);
  694. })
  695. });
  696. },
  697. 'abortUploadTask 抛弃分块上传任务': function() {
  698. cos.abortUploadTask({
  699. Bucket: config.Bucket,
  700. /* 必须 */ // Bucket 格式:test-1250000000
  701. Region: config.Region,
  702. /* 必须 */
  703. // 格式1,删除单个上传任务
  704. // Level: 'task',
  705. // Key: '10mb.zip',
  706. // UploadId: '14985543913e4e2642e31db217b9a1a3d9b3cd6cf62abfda23372c8d36ffa38585492681e3',
  707. // 格式2,删除单个文件所有未完成上传任务
  708. Level: 'file',
  709. Key: '10mb.zip',
  710. // 格式3,删除 Bucket 下所有未完成上传任务
  711. // Level: 'bucket',
  712. }, requestCallback);
  713. },
  714. }
  715. var advanceObjectDao = {
  716. 'sliceUploadFile 分块上传': function() {
  717. var sliceUploadFile = function (file) {
  718. var key = file.name;
  719. cos.sliceUploadFile({
  720. Bucket: config.Bucket,
  721. Region: config.Region,
  722. Key: key,
  723. FilePath: file.path,
  724. FileSize: file.size,
  725. CacheControl: 'max-age=7200',
  726. Headers: {
  727. aa: 123,
  728. },
  729. Query: {
  730. bb: 123,
  731. },
  732. onTaskReady: function(taskId) {
  733. TaskId = taskId
  734. },
  735. onHashProgress: function(info) {
  736. console.log('check hash', JSON.stringify(info));
  737. },
  738. onProgress: function(info) {
  739. console.log(JSON.stringify(info));
  740. }
  741. }, requestCallback);
  742. };
  743. wx.chooseMessageFile({
  744. count: 10,
  745. type: 'all',
  746. success: function(res) {
  747. sliceUploadFile(res.tempFiles[0]);
  748. }
  749. });
  750. // wx.chooseVideo({
  751. // sourceType: ['album','camera'],
  752. // maxDuration: 60,
  753. // camera: 'back',
  754. // success(res) {
  755. // var name = res.tempFilePath.replace(/^.*?([^/]{32}\.\w+)$/, '$1');
  756. // sliceUploadFile({
  757. // name: name,
  758. // path: res.tempFilePath,
  759. // size: res.size,
  760. // });
  761. // },
  762. // fail(err) {
  763. // console.log(err);
  764. // }
  765. // })
  766. },
  767. 'sliceCopyFile 分块复制对象': function() {
  768. // 创建测试文件
  769. var sourceName = '1.txt';
  770. var Key = '1.slicecopy.exe';
  771. var sourcePath = config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + sourceName;
  772. cos.sliceCopyFile({
  773. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  774. Region: config.Region,
  775. Key: Key,
  776. CopySource: sourcePath,
  777. SliceSize: 20 * 1024 * 1024, // 大于20M的文件用分片复制,小于则用单片复制
  778. onProgress: function(info) {
  779. var percent = parseInt(info.percent * 10000) / 100;
  780. var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
  781. console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
  782. }
  783. }, requestCallback);
  784. },
  785. cancelTask: function() {
  786. cos.cancelTask(TaskId);
  787. console.log('canceled');
  788. },
  789. pauseTask: function() {
  790. cos.pauseTask(TaskId);
  791. console.log('paused');
  792. },
  793. restartTask: function() {
  794. cos.restartTask(TaskId);
  795. console.log('restart');
  796. },
  797. };
  798. var ciObjectDao = {
  799. '上传时使用图片处理': function(){
  800. wx.chooseMessageFile({
  801. count: 10,
  802. type: 'all',
  803. success: function (res) {
  804. var file = res.tempFiles[0];
  805. wxfs.readFile({
  806. filePath: file.path,
  807. success: function (res) {
  808. cos.putObject({
  809. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  810. Region: config.Region,
  811. Key: file.name,
  812. Body: res.data,
  813. Headers: {
  814. // 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩
  815. 'Pic-Operations':
  816. '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}',
  817. },
  818. },
  819. requestCallback,
  820. );
  821. },
  822. fail: (err) => console.error(err),
  823. });
  824. },
  825. fail: (err) => console.error(err),
  826. });
  827. },
  828. '对云上数据进行图片处理': function(){
  829. // 对云上数据进行图片处理
  830. cos.request({
  831. Bucket: config.Bucket,
  832. Region: config.Region,
  833. Key: 'photo.png',
  834. Method: 'POST',
  835. Action: 'image_process',
  836. Headers: {
  837. // 通过 imageMogr2 接口使用图片缩放功能:指定图片宽度为 200,宽度等比压缩
  838. 'Pic-Operations': '{"is_pic_info": 1, "rules": [{"fileid": "desample_photo.jpg", "rule": "imageMogr2/thumbnail/200x/"}]}'
  839. },
  840. }, requestCallback);
  841. },
  842. '下载时使用图片处理': function(){
  843. cos.getObject({
  844. Bucket: config.Bucket,
  845. Region: config.Region,
  846. Key: '1.png',
  847. QueryString: `imageMogr2/thumbnail/200x/`,
  848. }, requestCallback);
  849. },
  850. '生成带图片处理参数的签名 URL': function(){
  851. // 生成带图片处理参数的文件签名URL,过期时间设置为 30 分钟。
  852. cos.getObjectUrl({
  853. Bucket: config.Bucket,
  854. Region: config.Region,
  855. Key: 'photo.png',
  856. QueryString: `imageMogr2/thumbnail/200x/`,
  857. Expires: 1800,
  858. Sign: true,
  859. },
  860. (err, data) => {
  861. console.log('带签名', err || data);
  862. });
  863. // 生成带图片处理参数的文件URL,不带签名。
  864. cos.getObjectUrl({
  865. Bucket: config.Bucket,
  866. Region: config.Region,
  867. Key: 'photo.png',
  868. QueryString: `imageMogr2/thumbnail/200x/`,
  869. Sign: false,
  870. },
  871. (err, data) => {
  872. console.log('不带签名', err || data);
  873. });
  874. },
  875. 'describeMediaBuckets 查询已经开通数据万象功能的存储桶': function() {
  876. var host = 'ci.' + config.Region + '.myqcloud.com';
  877. var url = 'https://' + host + '/mediabucket';
  878. cos.request({
  879. Bucket: config.Bucket,
  880. Region: config.Region,
  881. Method: 'GET',
  882. Key: 'mediabucket', /** 固定值,必须 */
  883. Url: url,
  884. Query: {
  885. pageNumber: '1', /** 第几页,非必须 */
  886. pageSize: '10', /** 每页个数,非必须 */
  887. // regions: 'ap-chengdu', /** 地域信息,例如'ap-beijing',支持多个值用逗号分隔如'ap-shanghai,ap-beijing',非必须 */
  888. // bucketNames: 'test-1250000000', /** 存储桶名称,精确搜索,例如'test-1250000000',支持多个值用逗号分隔如'test1-1250000000,test2-1250000000',非必须 */
  889. // bucketName: 'test', /** 存储桶名称前缀,前缀搜索,例如'test',支持多个值用逗号分隔如'test1,test2',非必须 */
  890. }
  891. },
  892. function(err, data){
  893. console.log(err || data);
  894. });
  895. },
  896. 'getMediaInfo 获取媒体文件信息': function() {
  897. cos.request({
  898. Bucket: config.Bucket,
  899. Region: config.Region,
  900. Method: 'GET',
  901. Key: 'test.mp4',
  902. Query: {
  903. 'ci-process': 'videoinfo' /** 固定值,必须 */
  904. }
  905. },
  906. function(err, data){
  907. console.log(err || data);
  908. });
  909. },
  910. 'getSnapshot 获取媒体文件某个时间的截图': function() {
  911. cos.request({
  912. Bucket: config.Bucket,
  913. Region: config.Region,
  914. Method: 'GET',
  915. Key: 'test.mp4',
  916. Query: {
  917. 'ci-process': 'snapshot', /** 固定值,必须 */
  918. time: 1, /** 截图的时间点,单位为秒,必须 */
  919. // width: 0, /** 截图的宽,非必须 */
  920. // height: 0, /** 截图的高,非必须 */
  921. // format: 'jpg', /** 截图的格式,支持 jpg 和 png,默认 jpg,非必须 */
  922. // rotate: 'auto', /** 图片旋转方式,默认为'auto',非必须 */
  923. // mode: 'exactframe', /** 截帧方式,默认为'exactframe',非必须 */
  924. },
  925. RawBody: true,
  926. },
  927. function(err, data){
  928. console.log(err || data);
  929. });
  930. },
  931. '图片同步审核 getImageAuditing': function () {
  932. cos.request({
  933. Bucket: config.Bucket,
  934. Region: config.Region,
  935. Method: 'GET',
  936. Key: '1.png',
  937. Query: {
  938. 'ci-process': 'sensitive-content-recognition', /** 固定值,必须 */
  939. 'biz-type': '', /** 审核类型,非必须 */
  940. 'detect-type': 'porn,ads', /** 审核策略,不填写则使用默认策略,非必须 */
  941. 'detect-url': '', /** 审核任意公网可访问的图片链接,非必须 */
  942. 'interval': 5, /** 审核 GIF 动图时,每隔interval帧截取一帧,非必须 */
  943. 'max-frames': 5, /** 审核 GIF 动图时,最大截帧数,非必须 */
  944. 'large-image-detect': '0', /** 是否需要压缩图片后再审核,非必须 */
  945. },
  946. },
  947. function(err, data){
  948. console.log(err || data);
  949. });
  950. },
  951. '图片批量审核 postImagesAuditing': function () {
  952. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  953. var url = 'https://' + host + '/image/auditing';
  954. var body = COS.util.json2xml({
  955. Request: {
  956. Input: [{
  957. Object: '1.png',
  958. }, {
  959. Object: '6.png',
  960. }],
  961. Conf: {
  962. BizType: '',
  963. DetectType: 'Porn'
  964. }
  965. }
  966. });
  967. cos.request({
  968. Bucket: config.Bucket,
  969. Region: config.Region,
  970. Method: 'POST',
  971. Url: url,
  972. Key: '/image/auditing',
  973. ContentType: 'application/xml',
  974. Body: body
  975. },
  976. function(err, data){
  977. console.log(err || data);
  978. });
  979. },
  980. '查询图片审核任务结果 getImageAuditingResult': function () {
  981. var jobId = 'si8263213daf3711eca0d1525400d88xxx'; // jobId可以通过图片批量审核返回
  982. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  983. var url = 'https://' + host + '/image/auditing/' + jobId;
  984. cos.request({
  985. Bucket: config.Bucket,
  986. Region: config.Region,
  987. Method: 'GET',
  988. Key: '/image/auditing/' + jobId,
  989. Url: url,
  990. },
  991. function(err, data){
  992. console.log(err || data);
  993. });
  994. },
  995. '提交视频审核任务 postVideoAuditing': function() {
  996. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  997. var url = 'https://' + host + '/video/auditing';
  998. var body = COS.util.json2xml({
  999. Request: {
  1000. Input: {
  1001. Object: '1.mp4',
  1002. },
  1003. Conf: {
  1004. BizType: '',
  1005. DetectType: 'Porn',
  1006. Snapshot: {
  1007. Count: 1000, // 视频截帧数量
  1008. },
  1009. DetectContent: 1, // 是否审核视频声音,0-只审核视频不审核声音;1-审核视频+声音
  1010. }
  1011. }
  1012. });
  1013. cos.request({
  1014. Bucket: config.Bucket,
  1015. Region: config.Region,
  1016. Method: 'POST',
  1017. Url: url,
  1018. Key: '/video/auditing',
  1019. ContentType: 'application/xml',
  1020. Body: body
  1021. },
  1022. function(err, data){
  1023. console.log(err || data);
  1024. });
  1025. },
  1026. '查询视频审核任务结果 getVideoAuditingResult': function () {
  1027. var jobId = 'av5bd873d9f39011ecb6c95254009a49da'; // jobId可以通过提交视频审核任务返回
  1028. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1029. var url = 'https://' + host + '/video/auditing/' + jobId;
  1030. cos.request({
  1031. Bucket: config.Bucket,
  1032. Region: config.Region,
  1033. Method: 'GET',
  1034. Key: '/video/auditing/' + jobId,
  1035. Url: url,
  1036. },
  1037. function(err, data){
  1038. console.log(err || data);
  1039. });
  1040. },
  1041. '提交音频审核任务 postAudioAuditing': function () {
  1042. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1043. var url = 'https://' + host + '/audio/auditing';
  1044. var body = COS.util.json2xml({
  1045. Request: {
  1046. Input: {
  1047. Object: '1.mp3',
  1048. },
  1049. Conf: {
  1050. BizType: '',
  1051. DetectType: 'Porn',
  1052. }
  1053. }
  1054. });
  1055. cos.request({
  1056. Bucket: config.Bucket,
  1057. Region: config.Region,
  1058. Method: 'POST',
  1059. Url: url,
  1060. Key: '/audio/auditing',
  1061. ContentType: 'application/xml',
  1062. Body: body
  1063. },
  1064. function(err, data){
  1065. console.log(err || data);
  1066. });
  1067. },
  1068. '查询音频审核任务结果 getAudioAuditingResult': function () {
  1069. var jobId = 'sa0c28d41daff411ecb23352540078cxxx'; // jobId可以通过提交音频审核任务返回
  1070. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1071. var url = 'https://' + host + '/audio/auditing/' + jobId;
  1072. cos.request({
  1073. Bucket: config.Bucket,
  1074. Region: config.Region,
  1075. Method: 'GET',
  1076. Key: '/audio/auditing/' + jobId,
  1077. Url: url,
  1078. },
  1079. function(err, data){
  1080. console.log(err || data);
  1081. });
  1082. },
  1083. '提交文本审核任务 postTextAuditing': function () {
  1084. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1085. var url = 'https://' + host + '/text/auditing';
  1086. var body = COS.util.json2xml({
  1087. Request: {
  1088. Input: {
  1089. // Object: 'hello.txt', // 存在cos里的资源,审核结果异步返回,可以调用查询文本审核结果api查询
  1090. Content: '5Lmz5rKf', // 经过base64编码过的文本”乳沟“,查询结果同步返回
  1091. },
  1092. Conf: {
  1093. BizType: '',
  1094. DetectType: 'Porn',
  1095. }
  1096. }
  1097. });
  1098. cos.request({
  1099. Bucket: config.Bucket,
  1100. Region: config.Region,
  1101. Method: 'POST',
  1102. Url: url,
  1103. Key: '/text/auditing',
  1104. ContentType: 'application/xml',
  1105. Body: body
  1106. },
  1107. function(err, data){
  1108. console.log(err || data);
  1109. });
  1110. },
  1111. '查询文本审核任务结果 getTextAuditingResult': function () {
  1112. var jobId = 'st8d88c664aff511ecb23352540078cxxx'; // jobId可以通过提交文本审核任务返回(Input传入Object)
  1113. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1114. var url = 'https://' + host + '/text/auditing/' + jobId;
  1115. cos.request({
  1116. Bucket: config.Bucket,
  1117. Region: config.Region,
  1118. Method: 'GET',
  1119. Key: '/text/auditing/' + jobId,
  1120. Url: url,
  1121. },
  1122. function(err, data){
  1123. console.log(err || data);
  1124. });
  1125. },
  1126. '提交文档审核任务 postDocumentAuditing': function () {
  1127. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1128. var url = 'https://' + host + '/document/auditing';
  1129. var body = COS.util.json2xml({
  1130. Request: {
  1131. Input: {
  1132. Object: 'test.xlsx', // 存在cos里的资源,审核结果异步返回,可以调用查询文本审核结果api查询
  1133. },
  1134. Conf: {
  1135. BizType: '',
  1136. DetectType: 'Porn',
  1137. }
  1138. }
  1139. });
  1140. cos.request({
  1141. Bucket: config.Bucket,
  1142. Region: config.Region,
  1143. Method: 'POST',
  1144. Url: url,
  1145. Key: '/document/auditing',
  1146. ContentType: 'application/xml',
  1147. Body: body
  1148. },
  1149. function(err, data){
  1150. console.log(err || data);
  1151. });
  1152. },
  1153. '查询文档审核任务结果 getDocumentAuditingResult': function () {
  1154. var jobId = 'sd7815c21caff611eca12f525400d88560'; // jobId可以通过提交文档审核任务返回
  1155. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1156. var url = 'https://' + host + '/document/auditing/' + jobId;
  1157. cos.request({
  1158. Bucket: config.Bucket,
  1159. Region: config.Region,
  1160. Method: 'GET',
  1161. Key: '/document/auditing/' + jobId,
  1162. Url: url,
  1163. },
  1164. function(err, data){
  1165. console.log(err || data);
  1166. });
  1167. },
  1168. '提交网页审核任务 postWebpageAuditing': function () {
  1169. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1170. var url = 'https://' + host + '/webpage/auditing';
  1171. var body = COS.util.json2xml({
  1172. Request: {
  1173. Input: {
  1174. Url: 'https://cloud.tencent.com/', // 存在cos里的资源,审核结果异步返回,可以调用查询文本审核结果api查询
  1175. },
  1176. Conf: {
  1177. BizType: '',
  1178. DetectType: 'Porn,Ads',
  1179. }
  1180. }
  1181. });
  1182. cos.request({
  1183. Bucket: config.Bucket,
  1184. Region: config.Region,
  1185. Method: 'POST',
  1186. Url: url,
  1187. Key: '/webpage/auditing',
  1188. ContentType: 'application/xml',
  1189. Body: body
  1190. },
  1191. function(err, data){
  1192. console.log(err || data);
  1193. });
  1194. },
  1195. '查询网页审核任务结果 getWebpageAuditingResult': function () {
  1196. var jobId = 'shce868019aff611ecb1155254009a4xxx'; // jobId可以通过提交网页审核任务返回
  1197. var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com';
  1198. var url = 'https://' + host + '/webpage/auditing/' + jobId;
  1199. cos.request({
  1200. Bucket: config.Bucket,
  1201. Region: config.Region,
  1202. Method: 'GET',
  1203. Key: '/webpage/auditing/' + jobId,
  1204. Url: url,
  1205. },
  1206. function(err, data){
  1207. console.log(err || data);
  1208. });
  1209. },
  1210. }
  1211. // require('./test');
  1212. module.exports = {
  1213. toolsDao: toolsDao,
  1214. bucketDao: bucketDao,
  1215. objectDao: objectDao,
  1216. advanceObjectDao: advanceObjectDao,
  1217. ciObjectDao: ciObjectDao,
  1218. };