diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index 5befbadc..c185c005 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -833,6 +833,41 @@ public function signRtmpUrl($bucket, $channelName, $timeout = 60, $options = NUL return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items); } + /** + * Generates the signed pushing streaming url + * + * @param string $bucket bucket name + * @param string $channelName channel name + * @param int $expiration expiration time of the Url, unix epoch, since 1970.1.1 00.00.00 UTC + * @param array $options + * @throws OssException + * @return The signed pushing streaming url + */ + public function generatePresignedRtmpUrl($bucket, $channelName, $expiration, $options = NULL) + { + $this->precheckCommon($bucket, $channelName, $options, false); + $proto = 'rtmp://'; + $hostname = $this->generateHostname($bucket); + $cano_params = ''; + $query_items = array(); + $params = isset($options['params']) ? $options['params'] : array(); + uksort($params, 'strnatcasecmp'); + foreach ($params as $key => $value) { + $cano_params = $cano_params . $key . ':' . $value . "\n"; + $query_items[] = rawurlencode($key) . '=' . rawurlencode($value); + } + $resource = '/' . $bucket . '/' . $channelName; + + $string_to_sign = $expiration . "\n" . $cano_params . $resource; + $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true)); + + $query_items[] = 'OSSAccessKeyId=' . rawurlencode($this->accessKeyId); + $query_items[] = 'Expires=' . rawurlencode($expiration); + $query_items[] = 'Signature=' . rawurlencode($signature); + + return $proto . $hostname . '/live/' . $channelName . '?' . implode('&', $query_items); + } + /** * Precheck the CORS request. Before sending a CORS request, a preflight request (OPTIONS) is sent with the specific origin. * HTTP METHOD and headers information are sent to OSS as well for evaluating if the CORS request is allowed. @@ -2574,6 +2609,37 @@ public function signUrl($bucket, $object, $timeout = 60, $method = self::OSS_HTT return $this->auth($options); } + /** + * Sign URL with specified expiration time in seconds and HTTP method. + * The signed URL could be used to access the object directly. + * + * @param string $bucket + * @param string $object + * @param int $expiration expiration time of the Url, unix epoch, since 1970.1.1 00.00.00 UTC + * @param string $method + * @param array $options Key-Value array + * @return string + * @throws OssException + */ + public function generatePresignedUrl($bucket, $object, $expiration, $method = self::OSS_HTTP_GET, $options = NULL) + { + $this->precheckCommon($bucket, $object, $options); + //method + if (self::OSS_HTTP_GET !== $method && self::OSS_HTTP_PUT !== $method) { + throw new OssException("method is invalid"); + } + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_OBJECT] = $object; + $options[self::OSS_METHOD] = $method; + if (!isset($options[self::OSS_CONTENT_TYPE])) { + $options[self::OSS_CONTENT_TYPE] = ''; + } + $options[self::OSS_PREAUTH] = $expiration; + $options[self::OSS_DATE] = $expiration; + $this->setSignStsInUrl(true); + return $this->auth($options); + } + /** * validates options. Create a empty array if it's NULL. * diff --git a/tests/OSS/Tests/BucketLiveChannelTest.php b/tests/OSS/Tests/BucketLiveChannelTest.php index bed68b03..aaebadb1 100644 --- a/tests/OSS/Tests/BucketLiveChannelTest.php +++ b/tests/OSS/Tests/BucketLiveChannelTest.php @@ -195,6 +195,39 @@ public function testSignRtmpUrl() $this->assertEquals('playlist.m3u8', $query['playlistName']); } + public function testGetgenPreSignedRtmpUrlVsSignedRtmpUrl() + { + $channelName = '90475'; + $bucket = 'douyu'; + $url1 = '245'; + $url2 = '123'; + $expiration = 0; + + do { + $begin = time(); + $expiration = time() + 900; + $url1 = $this->client->generatePresignedRtmpUrl($bucket, $channelName, $expiration, array( + 'params' => array( + 'playlistName' => 'playlist.m3u8' + ) + )); + + $url2 = $this->client->signRtmpUrl($bucket, $channelName, 900, array( + 'params' => array( + 'playlistName' => 'playlist.m3u8' + ) + )); + + $end = time(); + + if ($begin == $end) + break; + usleep(500000); + } while (true); + $this->assertEquals($url1, $url1); + $this->assertTrue(strpos($url1, 'Expires='.$expiration) !== false); + } + public function testLiveChannelInfo() { $channelName = 'live-to-put-status'; diff --git a/tests/OSS/Tests/OssClientSignatureTest.php b/tests/OSS/Tests/OssClientSignatureTest.php index 197a7c92..7c64d86a 100644 --- a/tests/OSS/Tests/OssClientSignatureTest.php +++ b/tests/OSS/Tests/OssClientSignatureTest.php @@ -1,130 +1,167 @@ -ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__)); - $timeout = 3600; - try { - $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout); - } catch (OssException $e) { - $this->assertFalse(true); - } - - $request = new RequestCore($signedUrl); - $request->set_method('GET'); - $request->add_header('Content-Type', ''); - $request->send_request(); - $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); - $this->assertEquals(file_get_contents(__FILE__), $res->body); - } - - public function testGetSignedUrlForPuttingObject() - { - $object = "a.file"; - $timeout = 3600; - try { - $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT"); - $content = file_get_contents(__FILE__); - $request = new RequestCore($signedUrl); - $request->set_method('PUT'); - $request->add_header('Content-Type', ''); - $request->add_header('Content-Length', strlen($content)); - $request->set_body($content); - $request->send_request(); - $res = new ResponseCore($request->get_response_header(), - $request->get_response_body(), $request->get_response_code()); - $this->assertTrue($res->isOK()); - } catch (OssException $e) { - $this->assertFalse(true); - } - } - - public function testGetSignedUrlForPuttingObjectFromFile() - { - $file = __FILE__; - $object = "a.file"; - $timeout = 3600; - $options = array('Content-Type' => 'txt'); - try { - $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT", $options); - $request = new RequestCore($signedUrl); - $request->set_method('PUT'); - $request->add_header('Content-Type', 'txt'); - $request->set_read_file($file); - $request->set_read_stream_size(filesize($file)); - $request->send_request(); - $res = new ResponseCore($request->get_response_header(), - $request->get_response_body(), $request->get_response_code()); - $this->assertTrue($res->isOK()); - } catch (OssException $e) { - $this->assertFalse(true); - } - - } - - public function testSignedUrlWithException() - { - $file = __FILE__; - $object = "a.file"; - $timeout = 3600; - $options = array('Content-Type' => 'txt'); - try { - $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "POST", $options); - $this->assertTrue(false); - } catch (OssException $e) { - $this->assertTrue(true); - if (strpos($e, "method is invalid") == false) - { - $this->assertTrue(false); - } - } - } - - - public function tearDown() - { - $this->ossClient->deleteObject($this->bucket, "a.file"); - parent::tearDown(); - } - - public function setUp() - { - parent::setUp(); - /** - * 上传本地变量到bucket - */ - $object = "a.file"; - $content = file_get_contents(__FILE__); - $options = array( - OssClient::OSS_LENGTH => strlen($content), - OssClient::OSS_HEADERS => array( - 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT', - 'Cache-Control' => 'no-cache', - 'Content-Disposition' => 'attachment;filename=oss_download.log', - 'Content-Encoding' => 'utf-8', - 'Content-Language' => 'zh-CN', - 'x-oss-server-side-encryption' => 'AES256', - 'x-oss-meta-self-define-title' => 'user define meta info', - ), - ); - - try { - $this->ossClient->putObject($this->bucket, $object, $content, $options); - } catch (OssException $e) { - $this->assertFalse(true); - } - } -} +ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__)); + $timeout = 3600; + try { + $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout); + } catch (OssException $e) { + $this->assertFalse(true); + } + + $request = new RequestCore($signedUrl); + $request->set_method('GET'); + $request->add_header('Content-Type', ''); + $request->send_request(); + $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); + $this->assertEquals(file_get_contents(__FILE__), $res->body); + } + + public function testGetSignedUrlForPuttingObject() + { + $object = "a.file"; + $timeout = 3600; + try { + $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT"); + $content = file_get_contents(__FILE__); + $request = new RequestCore($signedUrl); + $request->set_method('PUT'); + $request->add_header('Content-Type', ''); + $request->add_header('Content-Length', strlen($content)); + $request->set_body($content); + $request->send_request(); + $res = new ResponseCore($request->get_response_header(), + $request->get_response_body(), $request->get_response_code()); + $this->assertTrue($res->isOK()); + } catch (OssException $e) { + $this->assertFalse(true); + } + } + + public function testGetSignedUrlForPuttingObjectFromFile() + { + $file = __FILE__; + $object = "a.file"; + $timeout = 3600; + $options = array('Content-Type' => 'txt'); + try { + $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT", $options); + $request = new RequestCore($signedUrl); + $request->set_method('PUT'); + $request->add_header('Content-Type', 'txt'); + $request->set_read_file($file); + $request->set_read_stream_size(filesize($file)); + $request->send_request(); + $res = new ResponseCore($request->get_response_header(), + $request->get_response_body(), $request->get_response_code()); + $this->assertTrue($res->isOK()); + } catch (OssException $e) { + $this->assertFalse(true); + } + + } + + public function testSignedUrlWithException() + { + $file = __FILE__; + $object = "a.file"; + $timeout = 3600; + $options = array('Content-Type' => 'txt'); + try { + $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "POST", $options); + $this->assertTrue(false); + } catch (OssException $e) { + $this->assertTrue(true); + if (strpos($e, "method is invalid") == false) + { + $this->assertTrue(false); + } + } + } + + function testGetgenPreSignedUrlForGettingObject() + { + $object = "a.file"; + $this->ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__)); + $expires = time() + 3600; + try { + $signedUrl = $this->ossClient->generatePresignedUrl($this->bucket, $object, $expires); + } catch (OssException $e) { + $this->assertFalse(true); + } + + $request = new RequestCore($signedUrl); + $request->set_method('GET'); + $request->add_header('Content-Type', ''); + $request->send_request(); + $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); + $this->assertEquals(file_get_contents(__FILE__), $res->body); + } + + function testGetgenPreSignedUrlVsSignedUrl() + { + $object = "object-vs.file"; + $signedUrl1 = '245'; + $signedUrl2 = '123'; + $expiration = 0; + + do { + usleep(500000); + $begin = time(); + $expiration = time() + 3600; + $signedUrl1 = $this->ossClient->generatePresignedUrl($this->bucket, $object, $expiration); + $signedUrl2 = $this->ossClient->signUrl($this->bucket, $object, 3600); + $end = time(); + } while ($begin != $end); + $this->assertEquals($signedUrl1, $signedUrl2); + $this->assertTrue(strpos($signedUrl1, 'Expires='.$expiration) !== false); + } + + public function tearDown() + { + $this->ossClient->deleteObject($this->bucket, "a.file"); + parent::tearDown(); + } + + public function setUp() + { + parent::setUp(); + /** + * 上传本地变量到bucket + */ + $object = "a.file"; + $content = file_get_contents(__FILE__); + $options = array( + OssClient::OSS_LENGTH => strlen($content), + OssClient::OSS_HEADERS => array( + 'Expires' => 'Fri, 28 Feb 2020 05:38:42 GMT', + 'Cache-Control' => 'no-cache', + 'Content-Disposition' => 'attachment;filename=oss_download.log', + 'Content-Encoding' => 'utf-8', + 'Content-Language' => 'zh-CN', + 'x-oss-server-side-encryption' => 'AES256', + 'x-oss-meta-self-define-title' => 'user define meta info', + ), + ); + + try { + $this->ossClient->putObject($this->bucket, $object, $content, $options); + } catch (OssException $e) { + $this->assertFalse(true); + } + } +}