index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var Bucket = 'test-1250000000';
  2. var Region = 'ap-guangzhou';
  3. var cos = new COS({
  4. FileParallelLimit: 5,
  5. ChunkParallelLimit: 5,
  6. ChunkMbSize: 8 * 1024 * 1024,
  7. getAuthorization: function (options, callback) {
  8. var url = '/sts';
  9. var xhr = new XMLHttpRequest();
  10. xhr.open('GET', url, true);
  11. xhr.onload = function (e) {
  12. try {
  13. var data = JSON.parse(e.target.responseText);
  14. } catch (e) {
  15. }
  16. callback({
  17. TmpSecretId: data.credentials && data.credentials.tmpSecretId,
  18. TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
  19. SecurityToken: data.credentials && data.credentials.sessionToken,
  20. ExpiredTime: data.expiredTime
  21. });
  22. };
  23. xhr.send();
  24. }
  25. });
  26. new Vue({
  27. el: '#app',
  28. data: function () {
  29. return {
  30. FileParallelLimit: 5,
  31. ChunkParallelLimit: 16,
  32. ChunkMbSize: 2,
  33. list: [],
  34. total: 0,
  35. };
  36. },
  37. created: function () {
  38. var self = this;
  39. cos.on('list-update', function (data) {
  40. self.list = data.list;
  41. self.total = data.list.length;
  42. });
  43. },
  44. methods: {
  45. formatSize: function (size) {
  46. var i, unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  47. for (i = 0; i < unit.length && size >= 1024; i++) {
  48. size /= 1024;
  49. }
  50. return (Math.round(size * 100) / 100 || 0) + unit[i];
  51. },
  52. selectedFile: function (e) {
  53. var files = e.target.files;
  54. var list = [].map.call(files, function (f) {
  55. return {
  56. Bucket: Bucket,
  57. Region: Region,
  58. Key: f.name,
  59. Body: f,
  60. };
  61. });
  62. cos.uploadFiles({files: list});
  63. document.getElementById('form').reset();
  64. },
  65. pauseTask: function (task) {
  66. cos.pauseTask(task.id);
  67. },
  68. restartTask: function (task) {
  69. cos.restartTask(task.id);
  70. },
  71. cancelTask: function (task) {
  72. cos.cancelTask(task.id);
  73. },
  74. },
  75. });