Skip to content

Commit

Permalink
add new pre-signed url api
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Aug 31, 2020
1 parent 915e34d commit d314cbc
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 130 deletions.
66 changes: 66 additions & 0 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/OSS/Tests/BucketLiveChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading

0 comments on commit d314cbc

Please sign in to comment.