upload-download-sample.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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>upload_download_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="text" value="Hello OBS" id="stringToUpload"/>
  33. <input type="button" value="Click to upload object using string" onclick="uploadUsingString();"/>
  34. <p>
  35. <input type="file" id="fileToUpload"/>
  36. <input type="button" value="Click to upload object using file" onclick="uploadUsingFile();"/>
  37. <p>
  38. <input type="button" value="Click to download object as file" onclick="downloadAsFile();"/>
  39. <p>
  40. <input type="button" value="Click to download object as string" onclick="downloadAsString();"/>
  41. <p>
  42. <select id="saveAsByType">
  43. <option value="arraybuffer">arraybuffer</option>
  44. <option value="blob">blob</option>
  45. <option value="text">text</option>
  46. </select>
  47. <input type="button" value="Click to download object by type" onclick="downloadByType();"/>
  48. </body>
  49. <script src="../dist/esdk-obs-browserjs.3.23.5.min.js"></script>
  50. <script type="text/javascript">
  51. /**
  52. * This sample demonstrates how to upload/download an object
  53. * to/from OBS in different ways using the OBS SDK for BrowserJS.
  54. */
  55. function getObsClient(){
  56. /*
  57. * Initialize a obs client instance with your account for accessing OBS
  58. */
  59. var ak = document.getElementById('ak').value;
  60. var sk = document.getElementById('sk').value;
  61. var server = document.getElementById('server').value;
  62. return new ObsClient({
  63. access_key_id: ak,
  64. secret_access_key: sk,
  65. server : server,
  66. timeout : 60 * 5,
  67. });
  68. }
  69. /**
  70. * Upload an object by string
  71. */
  72. function uploadUsingString(){
  73. var bucketName = document.getElementById('bucketname').value;
  74. var objectKey = document.getElementById('objectkey').value;
  75. var obs = getObsClient();
  76. obs.putObject({
  77. Bucket: bucketName,
  78. Key : objectKey,
  79. Body : document.getElementById('stringToUpload').value
  80. }).then(function(result) {
  81. if(result.CommonMsg.Status < 300){
  82. console.log('Create object:' + objectKey + ' successfully!\n');
  83. }
  84. });
  85. }
  86. /**
  87. * Upload an object by file
  88. */
  89. function uploadUsingFile(){
  90. var bucketName = document.getElementById('bucketname').value;
  91. var objectKey = document.getElementById('objectkey').value;
  92. var obs = getObsClient();
  93. obs.putObject({
  94. Bucket: bucketName,
  95. Key : objectKey,
  96. SourceFile : document.getElementById('fileToUpload').files[0]
  97. }).then(function(result) {
  98. if(result.CommonMsg.Status < 300){
  99. console.log('Create object:' + objectKey + ' successfully!\n');
  100. }
  101. });
  102. }
  103. /*
  104. * Download the object to a file
  105. */
  106. function downloadByType(){
  107. var bucketName = document.getElementById('bucketname').value;
  108. var objectKey = document.getElementById('objectkey').value;
  109. var obs = getObsClient();
  110. obs.getObject({
  111. Bucket: bucketName,
  112. Key: objectKey,
  113. SaveByType: document.getElementById('saveAsByType').value
  114. }).then(function(result) {
  115. console.log(typeof result.InterfaceResult.Content);
  116. console.log('DownloadPath-->\n' + result.InterfaceResult.Content +'\n');
  117. });
  118. }
  119. /*
  120. * Download the object by type
  121. */
  122. function downloadAsFile(){
  123. var bucketName = document.getElementById('bucketname').value;
  124. var objectKey = document.getElementById('objectkey').value;
  125. var obs = getObsClient();
  126. obs.getObject({
  127. Bucket: bucketName,
  128. Key: objectKey,
  129. SaveByType : 'file'
  130. }).then(function(result) {
  131. console.log(result.InterfaceResult.Content);
  132. console.log('DownloadPath-->\n' + result.InterfaceResult.Content.SignedUrl+'\n');
  133. });
  134. }
  135. /*
  136. * Download the object as a String
  137. */
  138. function downloadAsString(){
  139. var bucketName = document.getElementById('bucketname').value;
  140. var objectKey = document.getElementById('objectkey').value;
  141. var obs = getObsClient();
  142. obs.getObject({
  143. Bucket: bucketName,
  144. Key: objectKey,
  145. }).then(function(result) {
  146. if(result.CommonMsg.Status < 300){
  147. console.log('Get object content');
  148. console.log('Content-->\n' + result.InterfaceResult.Content);
  149. console.log('\n');
  150. }
  151. });
  152. }
  153. </script>
  154. </html>