Common.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. if (is_file(__DIR__ . '/../autoload.php')) {
  3. require_once __DIR__ . '/../autoload.php';
  4. }
  5. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  6. require_once __DIR__ . '/../vendor/autoload.php';
  7. }
  8. require_once __DIR__ . '/Config.php';
  9. use NOS\NosClient;
  10. use NOS\Core\NosException;
  11. class Common
  12. {
  13. const endPoint = Config::NOS_ENDPOINT;
  14. const accessKey = Config::NOS_ACCESS_ID;
  15. const accessKeySecret = Config::NOS_ACCESS_KEY;
  16. const bucket = Config::NOS_TEST_BUCKET;
  17. public static function getNosClient()
  18. {
  19. try {
  20. $nosClient = new NosClient(self::accessKey, self::accessKeySecret, self::endPoint);
  21. } catch (NosException $e) {
  22. printf(__FUNCTION__ . "creating NosClient instance: FAILED\n");
  23. printf($e->getMessage() . "\n");
  24. return null;
  25. }
  26. return $nosClient;
  27. }
  28. public static function getTestBucketName()
  29. {
  30. return self::bucket;
  31. }
  32. public static function createBucket()
  33. {
  34. $nosClient = self::getNosClient();
  35. if (is_null($nosClient))
  36. exit(1);
  37. try {
  38. $exist = $nosClient->doesBucketExist(self::bucket);
  39. } catch (NosException $e) {
  40. printf(__FUNCTION__ . ": FAILED\n");
  41. printf($e->getMessage() . "\n");
  42. return;
  43. }
  44. if ($exist) {
  45. return;
  46. }
  47. $acl = NosClient::NOS_ACL_TYPE_PUBLIC_READ;
  48. try {
  49. $nosClient->createBucket(self::bucket, $acl);
  50. } catch (NosException $e) {
  51. printf(__FUNCTION__ . ": FAILED\n");
  52. printf($e->getMessage() . "\n");
  53. return;
  54. }
  55. printf(__FUNCTION__ . ": OK\n");
  56. }
  57. public static function println($message)
  58. {
  59. if (! empty($message)) {
  60. echo strval($message) . "\n";
  61. }
  62. }
  63. }
  64. Common::createBucket();