s3_test.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. use Aws\S3\S3Client;
  3. require_once(dirname(__FILE__).'/3rdparty/autoload.php');
  4. $CONFIG = dirname(__FILE__).'/storage.config.php';
  5. include($CONFIG);
  6. echo "\nconnect to S3...\n";
  7. $bucket_name = $CONFIG['objectstore']['arguments']['bucket'];
  8. $s3 = new S3Client([
  9. 'version' => 'latest',
  10. 'endpoint' => $CONFIG['objectstore']['arguments']['hostname'].':'.$CONFIG['objectstore']['arguments']['port'],
  11. 'region' => $CONFIG['objectstore']['arguments']['region'],
  12. 'credentials' => [
  13. 'key' => $CONFIG['objectstore']['arguments']['key'],
  14. 'secret' => $CONFIG['objectstore']['arguments']['secret'],
  15. ],
  16. 'use_path_style_endpoint' => $CONFIG['objectstore']['arguments']['use_path_style']
  17. ]);
  18. echo "...succeed\n";
  19. /*
  20. $buckets = $s3->listBuckets([]);
  21. echo $buckets;
  22. echo "\n";
  23. try {
  24. foreach ($buckets['Buckets'] as $bucket){
  25. echo "{$bucket['Name']}\t{$bucket['CreationDate']}\n";
  26. }
  27. } catch (S3Exception $e) {
  28. echo $e->getMessage();
  29. echo "\n";
  30. }
  31. echo "\n".
  32. "\nread files in S3...\n";
  33. echo "The contents of your bucket $bucket_name are: \n\n";
  34. $objects = S3list($s3, $bucket_name);
  35. echo 'S3list:'.print_r($objects);
  36. $result_s3 = S3put($s3, $bucket, [
  37. 'SourceFile' => './nextcloud_25.tar.gz',
  38. ]);
  39. echo 'S3put:'.$result_s3;
  40. */
  41. $bucket = 'nextcloud';
  42. $file_Path = './nextcloud_25.tar.gz';
  43. $key = basename($file_Path);
  44. echo "\nCopy file : ".$file_Path."\n";
  45. $start_time = microtime(true);
  46. try{
  47. $result = $s3->putObject([
  48. 'Bucket' => $bucket,
  49. 'Key' => $key,
  50. 'SourceFile' => $file_Path,
  51. 'ACL' => 'private',
  52. ]);
  53. } catch (S3Exception $e) {
  54. echo $e->getMessage() . "\n";
  55. }
  56. $end_time = microtime(true);
  57. $execution_time = ($end_time - $start_time);
  58. echo " Execution time of script = ".$execution_time." sec\n";
  59. echo 'S3put:'.$result_s3;
  60. //#########################################################################################
  61. function S3list($s3, $bucket, $maxIteration = 10000000) {
  62. $objects = [];
  63. try {
  64. $iteration = 0;
  65. $marker = '';
  66. do {
  67. //$result = $s3->listObjectsV2(['Bucket' => $bucket, 'Marker' => $marker]);
  68. $result = $s3->listObjectsV2(['Bucket' => $bucket]);
  69. if (rand(0,100) > 75 ) { echo '.'; }
  70. if ($result->get('Contents')) {
  71. $objects = array_merge($objects, $result->get('Contents'));
  72. }
  73. if (count($objects)) {
  74. $marker = $objects[count($objects) - 1]['Key'];
  75. }
  76. } while ($result->get('IsTruncated') && ++$iteration < $maxIteration);
  77. if ($result->get('IsTruncated')) {
  78. echo "\n".'WARNING: The number of keys greater than '.count($objects).' (the first part is loaded)';
  79. }
  80. return $objects;
  81. } catch (S3Exception $e) {
  82. return 'ERROR: Cannot retrieve objects: '.$e->getMessage();
  83. }
  84. }
  85. //#########################################################################################
  86. function S3put($s3, $bucket, $vars = array() ) {
  87. #return 'dummy';
  88. if (is_string($vars) ) {
  89. if (file_exists($vars)) {
  90. $vars = array('SourceFile' => $vars);
  91. }
  92. else {
  93. return 'ERROR: S3put($cms, $bucket, $vars)';
  94. }
  95. }
  96. if (empty($vars['Bucket']) ) { $vars['Bucket'] = $bucket; }
  97. if (empty($vars['Key'])
  98. && !empty($vars['SourceFile'])) { $vars['Key'] = $vars['SourceFile']; }
  99. if (empty($vars['ACL']) ) { $vars['ACL'] = 'private'; }
  100. if (empty($vars['Bucket']) ) { return 'ERROR: no Bucket'; }
  101. if (empty($vars['Key']) ) { return 'ERROR: no Key'; }
  102. if (!file_exists($vars['SourceFile'])) { return 'ERROR: file \''.$vars['SourceFile'].'\' does not exist'; }
  103. try {
  104. if (isset($GLOBALS['MULTIPART_THRESHOLD'])
  105. && filesize($vars['SourceFile']) > $GLOBALS['MULTIPART_THRESHOLD']*1024*1024) {
  106. $uploader = new MultipartUploader($s3,
  107. $vars['SourceFile'],
  108. $vars);
  109. $result = $uploader->upload();
  110. } else {
  111. if (filesize($vars['SourceFile']) > 2*1024*1024*1024) {
  112. echo "\n".'WARNING: file \''.$vars['SourceFile'].'\' is larger then 2 Gb, consider enabeling \'MultipartUploader\'';
  113. }
  114. print_r($vars);
  115. $result = $s3->putObject($vars);
  116. }
  117. if (!empty($result['ObjectURL'])) {
  118. return 'OK: '.'ObjectURL:'.$result['ObjectURL'];
  119. } else {
  120. return 'ERROR: '.$vars['Key'].' was not uploaded';
  121. }
  122. } catch (MultipartUploadException | S3Exception | Exception $e) {
  123. return 'ERROR: ' . $e->getMessage();
  124. }
  125. }