simple-multipart-upload-sample.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!--
  2. * Copyright 2019 Huawei Technologies Co.,Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  4. * this file except in compliance with the License. You may obtain a copy of the
  5. * License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed
  10. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  11. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  12. * specific language governing permissions and limitations under the License.
  13. -->
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="utf-8">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <meta http-equiv="Pragma" name="Pragma" content="no-cache">
  20. <title>simple_multipart_upload_sample</title>
  21. </head>
  22. <body>
  23. <h3>OBS BrowserJS SDK demo</h3>
  24. <h4>Basic Configuration</h4>
  25. <label>AK:</label> <input type="text" value="*** Provide your Access Key ***" id="ak" size="30"/>
  26. <label>SK:</label> <input type="text" value="*** Provide your Secret Key ***" id="sk" size="30"/>
  27. <p>
  28. <label>Server:</label> <input type="text" value="http://your-endpoint" id="server" size="30"/>
  29. <label>Bucket:</label> <input type="text" value="bucketname" id="bucketname"/>
  30. <label>Key:</label> <input type="text" value="objectkey" id="objectkey"/>
  31. <p>
  32. <input type="button" value="Click to do multipart upload" onclick="doMultipartUpload();"/>
  33. </body>
  34. <script src="../dist/esdk-obs-browserjs.3.23.5.min.js"></script>
  35. <script type="text/javascript">
  36. /**
  37. * This sample demonstrates how to upload multiparts to OBS
  38. * using the OBS SDK for BrowserJS.
  39. */
  40. function getObsClient(){
  41. /*
  42. * Initialize a obs client instance with your account for accessing OBS
  43. */
  44. var ak = document.getElementById('ak').value;
  45. var sk = document.getElementById('sk').value;
  46. var server = document.getElementById('server').value;
  47. return new ObsClient({
  48. access_key_id: ak,
  49. secret_access_key: sk,
  50. server : server,
  51. timeout : 60 * 5,
  52. });
  53. }
  54. function doMultipartUpload(){
  55. var bucketName = document.getElementById('bucketname').value;
  56. var objectKey = document.getElementById('objectkey').value;
  57. var obs = getObsClient();
  58. /*
  59. * Step 1: initiate multipart upload
  60. */
  61. console.log('Step 1: initiate multipart upload \n');
  62. obs.initiateMultipartUpload({
  63. Bucket: bucketName,
  64. Key: objectKey,
  65. }).then(function(result) {
  66. if(result.CommonMsg.Status < 300){
  67. var uploadId = result.InterfaceResult.UploadId;
  68. /*
  69. * Step 2: upload a part
  70. */
  71. console.log('Step 2: upload part \n');
  72. obs.uploadPart({
  73. Bucket: bucketName,
  74. Key: objectKey,
  75. UploadId: uploadId,
  76. PartNumber : 1,
  77. Body : 'Hello OBS'
  78. }).then(function(result) {
  79. if(result.CommonMsg.Status < 300){
  80. var etag = result.InterfaceResult.ETag;
  81. /*
  82. * Step 3: complete multipart upload
  83. */
  84. console.log('Step 3: complete multipart upload \n');
  85. obs.completeMultipartUpload({
  86. Bucket : bucketName,
  87. Key : objectKey,
  88. UploadId: uploadId,
  89. Parts : [{PartNumber : 1, ETag: etag}]
  90. }).then(function(result) {
  91. if(result.CommonMsg.Status < 300){
  92. console.log('complete multipart upload finished.\n');
  93. }
  94. });
  95. }
  96. });
  97. }
  98. });
  99. }
  100. </script>
  101. </html>