Signature.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. require_once __DIR__ . '/Common.php';
  3. use OSS\Http\RequestCore;
  4. use OSS\Http\ResponseCore;
  5. use OSS\OssClient;
  6. use OSS\Core\OssException;
  7. $bucket = Common::getBucketName();
  8. $ossClient = Common::getOssClient();
  9. if (is_null($ossClient)) exit(1);
  10. //******************************* Simple Usage ***************************************************************
  11. $ossClient->uploadFile($bucket, "a.file", __FILE__);
  12. // Generate a signed url for getting an object. The URL can be used in browser directly to download the file.
  13. $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600);
  14. Common::println($signedUrl);
  15. // Generate the signed url for putting an object. User can use put method with this url to upload a file to "a.file".
  16. $signedUrl = $ossClient->signUrl($bucket, "a.file", "3600", "PUT");
  17. Common::println($signedUrl);
  18. // Generate the signed url for putting an object from local file. The url can be used directly to upload the file to "a.file".
  19. $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600, "PUT", array('Content-Type' => 'txt'));
  20. Common::println($signedUrl);
  21. //******************************* For complete usage, see the following functions ****************************************************
  22. getSignedUrlForPuttingObject($ossClient, $bucket);
  23. getSignedUrlForPuttingObjectFromFile($ossClient, $bucket);
  24. getSignedUrlForGettingObject($ossClient, $bucket);
  25. /**
  26. * Generate the signed url for getObject() to control read accesses under private privilege
  27. *
  28. * @param $ossClient OssClient OssClient instance
  29. * @param $bucket string bucket name
  30. * @return null
  31. */
  32. function getSignedUrlForGettingObject($ossClient, $bucket)
  33. {
  34. $object = "test/test-signature-test-upload-and-download.txt";
  35. $timeout = 3600;
  36. try {
  37. $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);
  38. } catch (OssException $e) {
  39. printf(__FUNCTION__ . ": FAILED\n");
  40. printf($e->getMessage() . "\n");
  41. return;
  42. }
  43. print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
  44. /**
  45. * Use similar code to access the object by url, or use browser to access the object.
  46. */
  47. $request = new RequestCore($signedUrl);
  48. $request->set_method('GET');
  49. $request->add_header('Content-Type', '');
  50. $request->send_request();
  51. $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
  52. if ($res->isOK()) {
  53. print(__FUNCTION__ . ": OK" . "\n");
  54. } else {
  55. print(__FUNCTION__ . ": FAILED" . "\n");
  56. };
  57. }
  58. /**
  59. * Generate the signed url for PutObject to control write accesses under private privilege.
  60. *
  61. * @param OssClient $ossClient OssClient instance
  62. * @param string $bucket bucket name
  63. * @return null
  64. * @throws OssException
  65. */
  66. function getSignedUrlForPuttingObject($ossClient, $bucket)
  67. {
  68. $object = "test/test-signature-test-upload-and-download.txt";
  69. $timeout = 3600;
  70. $options = NULL;
  71. try {
  72. $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT");
  73. } catch (OssException $e) {
  74. printf(__FUNCTION__ . ": FAILED\n");
  75. printf($e->getMessage() . "\n");
  76. return;
  77. }
  78. print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
  79. $content = file_get_contents(__FILE__);
  80. $request = new RequestCore($signedUrl);
  81. $request->set_method('PUT');
  82. $request->add_header('Content-Type', '');
  83. $request->add_header('Content-Length', strlen($content));
  84. $request->set_body($content);
  85. $request->send_request();
  86. $res = new ResponseCore($request->get_response_header(),
  87. $request->get_response_body(), $request->get_response_code());
  88. if ($res->isOK()) {
  89. print(__FUNCTION__ . ": OK" . "\n");
  90. } else {
  91. print(__FUNCTION__ . ": FAILED" . "\n");
  92. };
  93. }
  94. /**
  95. * Generate the signed url for PutObject's signed url. User could use the signed url to upload file directly.
  96. *
  97. * @param OssClient $ossClient OssClient instance
  98. * @param string $bucket bucket name
  99. * @throws OssException
  100. */
  101. function getSignedUrlForPuttingObjectFromFile($ossClient, $bucket)
  102. {
  103. $file = __FILE__;
  104. $object = "test/test-signature-test-upload-and-download.txt";
  105. $timeout = 3600;
  106. $options = array('Content-Type' => 'txt');
  107. try {
  108. $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);
  109. } catch (OssException $e) {
  110. printf(__FUNCTION__ . ": FAILED\n");
  111. printf($e->getMessage() . "\n");
  112. return;
  113. }
  114. print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
  115. $request = new RequestCore($signedUrl);
  116. $request->set_method('PUT');
  117. $request->add_header('Content-Type', 'txt');
  118. $request->set_read_file($file);
  119. $request->set_read_stream_size(filesize($file));
  120. $request->send_request();
  121. $res = new ResponseCore($request->get_response_header(),
  122. $request->get_response_body(), $request->get_response_code());
  123. if ($res->isOK()) {
  124. print(__FUNCTION__ . ": OK" . "\n");
  125. } else {
  126. print(__FUNCTION__ . ": FAILED" . "\n");
  127. };
  128. }