CommandToRequestTransformer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Qcloud\Cos;
  3. use Guzzle\Service\Description\Parameter;
  4. use Guzzle\Service\Description\ServiceDescription;
  5. use GuzzleHttp\HandlerStack;
  6. use Psr\Http\Message\RequestInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Qcloud\Cos\Signature;
  9. use GuzzleHttp\Command\Guzzle\Description;
  10. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  11. use GuzzleHttp\Command\CommandInterface;
  12. use GuzzleHttp\Exception\RequestException;
  13. use GuzzleHttp\Middleware;
  14. use GuzzleHttp\Psr7;
  15. use GuzzleHttp\Psr7\Uri;
  16. use InvalidArgumentException;
  17. class CommandToRequestTransformer {
  18. private $config;
  19. private $operation;
  20. public function __construct($config ,$operation) {
  21. $this->config = $config;
  22. $this->operation = $operation;
  23. }
  24. // format bucket style
  25. public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) {
  26. $action = $command->getName();
  27. if ($action == 'ListBuckets') {
  28. return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/"));
  29. }
  30. $operation = $this->operation;
  31. $bucketname = $command['Bucket'];
  32. $appId = $this->config['appId'];
  33. if ($appId != null && endWith($bucketname, '-'.$appId) == False)
  34. {
  35. $bucketname = $bucketname.'-'.$appId;
  36. }
  37. $command['Bucket'] = $bucketname;
  38. $path = '';
  39. $http_method = $operation['httpMethod'];
  40. $uri = $operation['uri'];
  41. // Hoststyle is used by default
  42. // Pathstyle
  43. if ($this->config['pathStyle'] != true) {
  44. if (isset($operation['parameters']['Bucket']) && $command->hasParam('Bucket')) {
  45. $uri = str_replace("{Bucket}", '', $uri);
  46. }
  47. if (isset($operation['parameters']['Key']) && $command->hasParam('Key')) {
  48. $uri = str_replace("{/Key*}", encodeKey($command['Key']), $uri);
  49. }
  50. }
  51. $origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint'];
  52. // domain
  53. if ($this->config['domain'] != null) {
  54. $origin_host = $this->config['domain'];
  55. }
  56. $host = $origin_host;
  57. if ($this->config['ip'] != null) {
  58. $host = $this->config['ip'];
  59. if ($this->config['port'] != null) {
  60. $host = $this->config['ip'] . ":" . $this->config['port'];
  61. }
  62. }
  63. $path = $this->config['schema'].'://'. $host . $uri;
  64. $uri = new Uri($path);
  65. $query = $request->getUri()->getQuery();
  66. if ($uri->getQuery() != $query && $uri->getQuery() != "") {
  67. $query = $uri->getQuery() . "&" . $request->getUri()->getQuery();
  68. }
  69. $uri = $uri->withQuery($query);
  70. $request = $request->withUri($uri);
  71. $request = $request->withHeader('Host', $origin_host);
  72. return $request;
  73. }
  74. // format upload body
  75. public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') {
  76. $operation = $this->operation;
  77. if (!isset($operation['parameters']['Body'])) {
  78. return $request;
  79. }
  80. $source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null;
  81. $body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null;
  82. // If a file path is passed in then get the file handle
  83. if (is_string($source) && file_exists($source)) {
  84. $body = fopen($source, 'rb');
  85. }
  86. // Prepare the body parameter and remove the source file parameter
  87. if (null !== $body) {
  88. return $request;
  89. } else {
  90. throw new InvalidArgumentException(
  91. "You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters.");
  92. }
  93. }
  94. // update md5
  95. public function md5Transformer(CommandInterface $command, $request) {
  96. $operation = $this->operation;
  97. if (isset($operation['data']['contentMd5'])) {
  98. $request = $this->addMd5($request);
  99. }
  100. if (isset($operation['parameters']['ContentMD5']) &&
  101. isset($command['ContentMD5'])) {
  102. $value = $command['ContentMD5'];
  103. if ($value === true) {
  104. $request = $this->addMd5($request);
  105. }
  106. }
  107. return $request;
  108. }
  109. // add meta
  110. public function metadataTransformer(CommandInterface $command, $request) {
  111. $operation = $this->operation;
  112. if (isset($command['Metadata'])) {
  113. $meta = $command['Metadata'];
  114. foreach ($meta as $key => $value) {
  115. $request = $request->withHeader('x-cos-meta-' . $key, $value);
  116. }
  117. }
  118. $request = headersMap($command, $request);
  119. return $request;
  120. }
  121. // count md5
  122. private function addMd5($request) {
  123. $body = $request->getBody();
  124. if ($body && $body->getSize() > 0) {
  125. $md5 = base64_encode(md5($body, true));
  126. return $request->withHeader('Content-MD5', $md5);
  127. }
  128. return $request;
  129. }
  130. // inventoryId
  131. public function specialParamTransformer(CommandInterface $command, $request) {
  132. $action = $command->getName();
  133. if ($action == 'PutBucketInventory') {
  134. $id = $command['Id'];
  135. $uri = $request->getUri();
  136. $query = $uri->getQuery();
  137. $uri = $uri->withQuery($query . "&Id=".$id);
  138. return $request->withUri($uri);
  139. }
  140. return $request;
  141. }
  142. public function __destruct() {
  143. }
  144. }