OssClientSignatureTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Http\RequestCore;
  5. use OSS\Http\ResponseCore;
  6. use OSS\OssClient;
  7. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  8. class OssClientSignatureTest extends TestOssClientBase
  9. {
  10. function testGetSignedUrlForGettingObject()
  11. {
  12. $object = "a.file";
  13. $this->ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__));
  14. $timeout = 3600;
  15. try {
  16. $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout);
  17. } catch (OssException $e) {
  18. $this->assertFalse(true);
  19. }
  20. $request = new RequestCore($signedUrl);
  21. $request->set_method('GET');
  22. $request->add_header('Content-Type', '');
  23. $request->send_request();
  24. $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
  25. $this->assertEquals(file_get_contents(__FILE__), $res->body);
  26. }
  27. public function testGetSignedUrlForPuttingObject()
  28. {
  29. $object = "a.file";
  30. $timeout = 3600;
  31. try {
  32. $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT");
  33. $content = file_get_contents(__FILE__);
  34. $request = new RequestCore($signedUrl);
  35. $request->set_method('PUT');
  36. $request->add_header('Content-Type', '');
  37. $request->add_header('Content-Length', strlen($content));
  38. $request->set_body($content);
  39. $request->send_request();
  40. $res = new ResponseCore($request->get_response_header(),
  41. $request->get_response_body(), $request->get_response_code());
  42. $this->assertTrue($res->isOK());
  43. } catch (OssException $e) {
  44. $this->assertFalse(true);
  45. }
  46. }
  47. public function testGetSignedUrlForPuttingObjectFromFile()
  48. {
  49. $file = __FILE__;
  50. $object = "a.file";
  51. $timeout = 3600;
  52. $options = array('Content-Type' => 'txt');
  53. try {
  54. $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT", $options);
  55. $request = new RequestCore($signedUrl);
  56. $request->set_method('PUT');
  57. $request->add_header('Content-Type', 'txt');
  58. $request->set_read_file($file);
  59. $request->set_read_stream_size(filesize($file));
  60. $request->send_request();
  61. $res = new ResponseCore($request->get_response_header(),
  62. $request->get_response_body(), $request->get_response_code());
  63. $this->assertTrue($res->isOK());
  64. } catch (OssException $e) {
  65. $this->assertFalse(true);
  66. }
  67. }
  68. public function tearDown()
  69. {
  70. $this->ossClient->deleteObject($this->bucket, "a.file");
  71. parent::tearDown();
  72. }
  73. public function setUp()
  74. {
  75. parent::setUp();
  76. /**
  77. * 上传本地变量到bucket
  78. */
  79. $object = "a.file";
  80. $content = file_get_contents(__FILE__);
  81. $options = array(
  82. OssClient::OSS_LENGTH => strlen($content),
  83. OssClient::OSS_HEADERS => array(
  84. 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT',
  85. 'Cache-Control' => 'no-cache',
  86. 'Content-Disposition' => 'attachment;filename=oss_download.log',
  87. 'Content-Encoding' => 'utf-8',
  88. 'Content-Language' => 'zh-CN',
  89. 'x-oss-server-side-encryption' => 'AES256',
  90. 'x-oss-meta-self-define-title' => 'user define meta info',
  91. ),
  92. );
  93. try {
  94. $this->ossClient->putObject($this->bucket, $object, $content, $options);
  95. } catch (OssException $e) {
  96. $this->assertFalse(true);
  97. }
  98. }
  99. }