s3_test_multi.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. i<?php
  2. use Aws\S3\S3Client;
  3. use Aws\Exception\AwsException;
  4. use Aws\S3\ObjectUploader;
  5. use Aws\S3\MultipartUploader;
  6. use Aws\Exception\MultipartUploadException;
  7. require_once(dirname(__FILE__).'/3rdparty/autoload.php');
  8. $CONFIG = dirname(__FILE__).'/storage.config.php';
  9. include($CONFIG);
  10. echo "\nconnect to S3...\n";
  11. $bucket_name = $CONFIG['objectstore']['arguments']['bucket'];
  12. $s3Client = new S3Client([
  13. 'version' => 'latest',
  14. 'endpoint' => $CONFIG['objectstore']['arguments']['hostname'],
  15. 'region' => $CONFIG['objectstore']['arguments']['region'],
  16. 'credentials' => [
  17. 'key' => $CONFIG['objectstore']['arguments']['key'],
  18. 'secret' => $CONFIG['objectstore']['arguments']['secret'],
  19. ],
  20. 'use_path_style_endpoint' => $CONFIG['objectstore']['arguments']['use_path_style']
  21. ]);
  22. $bucket = 'nextcloud';
  23. $file_Path = '/storage/nextcloud/test_pedro.gpkg';
  24. $files = [$file_Path];
  25. $key = basename($file_Path);
  26. echo "\nPHP: copy file : ".$file_Path."\n";
  27. // Using stream instead of file path
  28. $source = fopen($file_Path, 'rb');
  29. $uploader = new ObjectUploader(
  30. $s3Client,
  31. $bucket,
  32. $key,
  33. $source,
  34. 'private',
  35. [ 'concurrency' => 5, 'part_size' => 1536*1024*1024 ]
  36. );
  37. $start_time = microtime(true);
  38. do {
  39. try {
  40. $result = $uploader->upload();
  41. if ($result["@metadata"]["statusCode"] == '200') {
  42. print('File successfully uploaded to ' . $result["ObjectURL"]);
  43. }
  44. } catch (MultipartUploadException $e) {
  45. rewind($source);
  46. $uploader = new MultipartUploader($s3Client, $source, [
  47. 'state' => $e->getState(),
  48. 'acl' => 'public-read',
  49. ]);
  50. }
  51. } while (!isset($result));
  52. fclose($source);
  53. $end_time = microtime(true);
  54. $execution_time = ($end_time - $start_time);
  55. echo "\nExecution time of script = ".$execution_time." sec\n";