Common.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/../../../autoload.php';
  4. use OSS\OssClient;
  5. use OSS\Core\OssException;
  6. /**
  7. * Class Common
  8. *
  9. * 示例程序【Samples/*.php】 的Common类,用于获取OssClient实例和其他公用方法
  10. */
  11. class Common
  12. {
  13. /**
  14. * 根据Config配置,得到一个OssClient实例
  15. *
  16. * @return OssClient 一个OssClient实例
  17. */
  18. public static function getOssClient()
  19. {
  20. try {
  21. $ossClient = new OssClient(
  22. getenv('OSS_ACCESS_KEY_ID'),
  23. getenv('OSS_ACCESS_KEY_SECRET'),
  24. getenv('OSS_ENDPOINT'), false);
  25. } catch (OssException $e) {
  26. printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
  27. printf($e->getMessage() . "\n");
  28. return null;
  29. }
  30. return $ossClient;
  31. }
  32. public static function getBucketName()
  33. {
  34. return getenv('OSS_BUCKET');
  35. }
  36. /**
  37. * 工具方法,创建一个bucket
  38. */
  39. public static function createBucket()
  40. {
  41. $ossClient = self::getOssClient();
  42. if (is_null($ossClient)) exit(1);
  43. $bucket = self::getBucketName();
  44. $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
  45. try {
  46. $ossClient->createBucket($bucket, $acl);
  47. } catch (OssException $e) {
  48. printf(__FUNCTION__ . ": FAILED\n");
  49. printf($e->getMessage() . "\n");
  50. return;
  51. }
  52. print(__FUNCTION__ . ": OK" . "\n");
  53. }
  54. /**
  55. * Wait for bucket meta sync
  56. */
  57. public static function waitMetaSync()
  58. {
  59. if (getenv('TRAVIS')) {
  60. sleep(10);
  61. }
  62. }
  63. }