Common.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace OSS\Tests;
  3. require_once __DIR__ . '/../../../autoload.php';
  4. require_once __DIR__ . DIRECTORY_SEPARATOR . 'StsClient.php';
  5. use OSS\OssClient;
  6. use OSS\Core\OssException;
  7. use OSS\Credentials\StaticCredentialsProvider;
  8. /**
  9. * Class Common
  10. *
  11. * Sample program [Samples / *. Php] Common class, used to obtain OssClient instance and other public methods
  12. */
  13. class Common
  14. {
  15. /**
  16. * According to the Config configuration, get an OssClient instance
  17. *
  18. * @return OssClient An OssClient instance
  19. */
  20. public static function getOssClient($conf = NULL)
  21. {
  22. try {
  23. $provider = new StaticCredentialsProvider(
  24. getenv('OSS_ACCESS_KEY_ID'),
  25. getenv('OSS_ACCESS_KEY_SECRET')
  26. );
  27. $config = array(
  28. 'region' => self::getRegion(),
  29. 'endpoint' => self::getEndpoint(),
  30. 'provider' => $provider,
  31. 'signatureVersion' => self::getSignVersion()
  32. );
  33. if ($conf != null) {
  34. foreach ($conf as $key => $value) {
  35. $config[$key] = $value;
  36. }
  37. }
  38. $ossClient = new OssClient($config);
  39. } catch (OssException $e) {
  40. printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
  41. printf($e->getMessage() . "\n");
  42. }
  43. return $ossClient;
  44. }
  45. public static function getStsOssClient($conf = NULL)
  46. {
  47. $stsClient = new StsClient();
  48. $assumeRole = new AssumeRole();
  49. $stsClient->AccessSecret = getenv('OSS_ACCESS_KEY_SECRET');
  50. $assumeRole->AccessKeyId = getenv('OSS_ACCESS_KEY_ID');
  51. $assumeRole->RoleArn = getenv('OSS_TEST_RAM_ROLE_ARN');
  52. $params = $assumeRole->getAttributes();
  53. $response = $stsClient->doAction($params);
  54. try {
  55. $provider = new StaticCredentialsProvider(
  56. $response->Credentials->AccessKeyId,
  57. $response->Credentials->AccessKeySecret,
  58. $response->Credentials->SecurityToken
  59. );
  60. $config = array(
  61. 'region' => self::getRegion(),
  62. 'endpoint' => self::getEndpoint(),
  63. 'provider' => $provider,
  64. 'signatureVersion' => self::getSignVersion()
  65. );
  66. if ($conf != null) {
  67. foreach ($conf as $key => $value) {
  68. $config[$key] = $value;
  69. }
  70. }
  71. $ossStsClient = new OssClient($config);
  72. } catch (OssException $e) {
  73. printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
  74. printf($e->getMessage() . "\n");
  75. return null;
  76. }
  77. return $ossStsClient;
  78. }
  79. public static function getBucketName()
  80. {
  81. $name = getenv('OSS_BUCKET');
  82. if (empty($name)) {
  83. return "skyranch-php-test";
  84. }
  85. return $name;
  86. }
  87. public static function getRegion()
  88. {
  89. return getenv('OSS_TEST_REGION');
  90. }
  91. public static function getEndpoint()
  92. {
  93. return getenv('OSS_TEST_ENDPOINT');
  94. }
  95. public static function getCallbackUrl()
  96. {
  97. return getenv('OSS_TEST_CALLBACK_URL');
  98. }
  99. public static function getPayerUid()
  100. {
  101. return getenv('OSS_TEST_PAYER_UID');
  102. }
  103. public static function getPayerAccessKeyId()
  104. {
  105. return getenv('OSS_TEST_PAYER_ACCESS_KEY_ID');
  106. }
  107. public static function getPayerAccessKeySecret()
  108. {
  109. return getenv('OSS_TEST_PAYER_ACCESS_KEY_SECRET');
  110. }
  111. public static function getSignVersion()
  112. {
  113. return OssClient::OSS_SIGNATURE_VERSION_V1;
  114. }
  115. public static function getPathStyleBucket()
  116. {
  117. return getenv('OSS_TEST_PATHSTYLE_BUCKET');
  118. }
  119. /**
  120. * Tool method, create a bucket
  121. */
  122. public static function createBucket()
  123. {
  124. $ossClient = self::getOssClient();
  125. if (is_null($ossClient)) exit(1);
  126. $bucket = self::getBucketName();
  127. $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
  128. try {
  129. $ossClient->createBucket($bucket, $acl);
  130. } catch (OssException $e) {
  131. printf(__FUNCTION__ . ": FAILED\n");
  132. printf($e->getMessage() . "\n");
  133. return;
  134. }
  135. print(__FUNCTION__ . ": OK" . "\n");
  136. }
  137. /**
  138. * Wait for bucket meta sync
  139. */
  140. public static function waitMetaSync()
  141. {
  142. if (getenv('TRAVIS')) {
  143. sleep(10);
  144. }
  145. }
  146. }