delete-objects-sample.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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>delete_objects_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. <p>
  31. <input type="button" value="Click to batch delete objects" onclick="deleteObjects();"/>
  32. </body>
  33. <script src="../dist/esdk-obs-browserjs.3.23.5.min.js"></script>
  34. <script type="text/javascript">
  35. /**
  36. * This sample demonstrates how to delete objects under specified bucket
  37. * from OBS using the OBS SDK for BrowserJS.
  38. */
  39. function getObsClient(){
  40. /*
  41. * Initialize a obs client instance with your account for accessing OBS
  42. */
  43. var ak = document.getElementById('ak').value;
  44. var sk = document.getElementById('sk').value;
  45. var server = document.getElementById('server').value;
  46. return new ObsClient({
  47. access_key_id: ak,
  48. secret_access_key: sk,
  49. server : server,
  50. timeout : 60 * 5
  51. });
  52. }
  53. function doDelete(keys){
  54. console.log('Deleting all objects\n');
  55. var bucketName = document.getElementById('bucketname').value;
  56. var obs = getObsClient();
  57. obs.deleteObjects({
  58. Bucket: bucketName,
  59. Quiet:false,
  60. Objects: keys
  61. }).then(function(result) {
  62. if(result.CommonMsg.Status < 300){
  63. console.log('Deleteds:');
  64. var i=0;
  65. for(;i<result.InterfaceResult.Deleteds.length;i++){
  66. console.log('Deleted[' + i + ']:');
  67. console.log('Key-->'+result.InterfaceResult.Deleteds[i]['Key']);
  68. console.log('VersionId-->' + result.InterfaceResult.Deleteds[i]['VersionId']);
  69. console.log('DeleteMarker-->' + result.InterfaceResult.Deleteds[i]['DeleteMarker']);
  70. console.log('DeleteMarkerVersionId-->' + result.InterfaceResult.Deleteds[i]['DeleteMarkerVersionId']);
  71. }
  72. console.log('\n');
  73. console.log('Errors:');
  74. i=0;
  75. for(;i<result.InterfaceResult.Errors.length;i++){
  76. console.log('Error[' + i + ']:');
  77. console.log('Key-->' + result.InterfaceResult.Errors[i]['Key']);
  78. console.log('VersionId-->' + result.InterfaceResult.Errors[i]['VersionId']);
  79. console.log('Code-->' + result.InterfaceResult.Errors[i]['Code']);
  80. console.log('Message-->' + result.InterfaceResult.Errors[i]['Message']);
  81. }
  82. }
  83. });
  84. }
  85. /*
  86. * Batch delete objects
  87. */
  88. function deleteObjects(){
  89. var keys = [];
  90. var bucketName = document.getElementById('bucketname').value;
  91. var obs = getObsClient();
  92. /*
  93. * Batch put objects into the bucket
  94. */
  95. var content = 'Thank you for using Object Storage Servic';
  96. var keyPrefix = 'MyObjectKey';
  97. var finishedCount = 0;
  98. var objectCount = 10;
  99. var i=0;
  100. for(;i<objectCount;i++){
  101. var key = keyPrefix + i;
  102. obs.putObject({
  103. Bucket : bucketName,
  104. Key : key,
  105. Body : content
  106. }).then((function(key){
  107. return function(result) {
  108. finishedCount++;
  109. if(result.CommonMsg.Status < 300){
  110. console.log('Succeed to put object:' + key);
  111. keys.push({Key:key});
  112. }
  113. if(finishedCount === objectCount){
  114. console.log('\n');
  115. doDelete(keys);
  116. }
  117. };
  118. })(key));
  119. }
  120. }
  121. </script>
  122. </html>