No Description

Signature.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. //******************************* 简单使用 ***************************************************************
  11. $ossClient->uploadFile($bucket, "a.file", __FILE__);
  12. // 生成GetObject的签名url,用户可以使用这个url直接在浏览器下载
  13. $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600);
  14. Common::println($signedUrl);
  15. // 生成用于putObject的签名URL,用户可以直接用put方法使用这个url上传文件到 "a.file"
  16. $signedUrl = $ossClient->signUrl($bucket, "a.file", "3600", "PUT");
  17. Common::println($signedUrl);
  18. // 生成从本地文件上传PutObject的签名url, 用户可以直接使用这个url把本地文件上传到 "a.file"
  19. $signedUrl = $ossClient->signUrl($bucket, "a.file", 3600, "PUT", array('Content-Type' => 'txt'));
  20. Common::println($signedUrl);
  21. //******************************* 完整用法参考下面函数 ****************************************************
  22. getSignedUrlForPuttingObject($ossClient, $bucket);
  23. getSignedUrlForPuttingObjectFromFile($ossClient, $bucket);
  24. getSignedUrlForGettingObject($ossClient, $bucket);
  25. /**
  26. * 生成GetObject的签名url,主要用于私有权限下的读访问控制
  27. *
  28. * @param $ossClient OssClient OssClient实例
  29. * @param $bucket string 存储空间名称
  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. * 可以类似的代码来访问签名的URL,也可以输入到浏览器中去访问
  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. * 生成PutObject的签名url,主要用于私有权限下的写访问控制
  60. *
  61. * @param OssClient $ossClient OssClient实例
  62. * @param string $bucket 存储空间名称
  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. * 生成PutObject的签名url,主要用于私有权限下的写访问控制, 用户可以利用生成的signedUrl
  96. * 从文件上传文件
  97. *
  98. * @param OssClient $ossClient OssClient实例
  99. * @param string $bucket 存储空间名称
  100. * @throws OssException
  101. */
  102. function getSignedUrlForPuttingObjectFromFile($ossClient, $bucket)
  103. {
  104. $file = __FILE__;
  105. $object = "test/test-signature-test-upload-and-download.txt";
  106. $timeout = 3600;
  107. $options = array('Content-Type' => 'txt');
  108. try {
  109. $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);
  110. } catch (OssException $e) {
  111. printf(__FUNCTION__ . ": FAILED\n");
  112. printf($e->getMessage() . "\n");
  113. return;
  114. }
  115. print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
  116. $request = new RequestCore($signedUrl);
  117. $request->set_method('PUT');
  118. $request->add_header('Content-Type', 'txt');
  119. $request->set_read_file($file);
  120. $request->set_read_stream_size(filesize($file));
  121. $request->send_request();
  122. $res = new ResponseCore($request->get_response_header(),
  123. $request->get_response_body(), $request->get_response_code());
  124. if ($res->isOK()) {
  125. print(__FUNCTION__ . ": OK" . "\n");
  126. } else {
  127. print(__FUNCTION__ . ": FAILED" . "\n");
  128. };
  129. }