如何在 AWS Cloudfront 中组合流名称?
How to combine stream name in AWS cloudfront?
Amazon Cloudfront SDK 和示例代码 - 如何组合流名称?所有其他功能都已详细说明,但我看不到 create_stream_name().
的任何定义或示例
下面是此处文档中描述的示例的完整代码:https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CreateURL_PHP.html
如你所见,函数create_stream_name()并没有出现在任何地方,而是在第12行被调用,这将导致"call to undefined function"错误。我应该如何定义 create_stream_name()?
function get_canned_policy_stream_name($video_path, $private_key_filename, $key_pair_id, $expires) {
// this policy is well known by CloudFront, but you still need to sign it,
// since it contains your parameters
$canned_policy = '{"Statement":[{"Resource":"' . $video_path . '","Condition":{"DateLessThan":{"AWS:EpochTime":'. $expires . '}}}]}';
// sign the canned policy
$signature = $this->rsa_sha1_sign($canned_policy, $private_key_filename);
// make the signature safe to be included in a url
$encoded_signature = $this->url_safe_base64_encode($signature);
// combine the above into a stream name
$stream_name = create_stream_name($video_path, null, $encoded_signature, $key_pair_id, $expires);
// url-encode the query string characters to work around a flash player bug
return encode_query_params($stream_name);
}
function rsa_sha1_sign($policy, $private_key_filename) {
$signature = "";
// load the private key
$fp = fopen($private_key_filename, "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
// compute signature
openssl_sign($policy, $signature, $pkeyid);
// free the key from memory
openssl_free_key($pkeyid);
return $signature;
}
function url_safe_base64_encode($value) {
$encoded = base64_encode($value);
// replace unsafe characters +, = and / with
// the safe characters -, _ and ~
return str_replace(
array('+', '=', '/'),
array('-', '_', '~'),
$encoded);
}
函数 create_stream_name()
在完整示例 (demo.php) 中定义,您可以在 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/samples/demo-php.zip 下载该示例。完整的示例 linked 位于文档页面的顶部,尽管它在文本 "Signature Code for Video Streaming in PHP." 后面有些模糊 [更新: 它不再模糊,请参阅下面。]
完全披露:我是该文档页面的负责人,所以我会做一些事情让它变得更好:
- 使完整示例的 link 更容易被发现。 [更新:完整示例的下载 URL 现在在 the documentation page 的顶部更加突出。]
- 将完整的示例添加到页面,这样您就可以直接在页面上查看和复制它,而不必下载 zip 文件。 [更新: 完整示例现在是 published at the bottom of the documentation page。]
Amazon Cloudfront SDK 和示例代码 - 如何组合流名称?所有其他功能都已详细说明,但我看不到 create_stream_name().
的任何定义或示例下面是此处文档中描述的示例的完整代码:https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CreateURL_PHP.html
如你所见,函数create_stream_name()并没有出现在任何地方,而是在第12行被调用,这将导致"call to undefined function"错误。我应该如何定义 create_stream_name()?
function get_canned_policy_stream_name($video_path, $private_key_filename, $key_pair_id, $expires) {
// this policy is well known by CloudFront, but you still need to sign it,
// since it contains your parameters
$canned_policy = '{"Statement":[{"Resource":"' . $video_path . '","Condition":{"DateLessThan":{"AWS:EpochTime":'. $expires . '}}}]}';
// sign the canned policy
$signature = $this->rsa_sha1_sign($canned_policy, $private_key_filename);
// make the signature safe to be included in a url
$encoded_signature = $this->url_safe_base64_encode($signature);
// combine the above into a stream name
$stream_name = create_stream_name($video_path, null, $encoded_signature, $key_pair_id, $expires);
// url-encode the query string characters to work around a flash player bug
return encode_query_params($stream_name);
}
function rsa_sha1_sign($policy, $private_key_filename) {
$signature = "";
// load the private key
$fp = fopen($private_key_filename, "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
// compute signature
openssl_sign($policy, $signature, $pkeyid);
// free the key from memory
openssl_free_key($pkeyid);
return $signature;
}
function url_safe_base64_encode($value) {
$encoded = base64_encode($value);
// replace unsafe characters +, = and / with
// the safe characters -, _ and ~
return str_replace(
array('+', '=', '/'),
array('-', '_', '~'),
$encoded);
}
函数 create_stream_name()
在完整示例 (demo.php) 中定义,您可以在 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/samples/demo-php.zip 下载该示例。完整的示例 linked 位于文档页面的顶部,尽管它在文本 "Signature Code for Video Streaming in PHP." 后面有些模糊 [更新: 它不再模糊,请参阅下面。]
完全披露:我是该文档页面的负责人,所以我会做一些事情让它变得更好:
- 使完整示例的 link 更容易被发现。 [更新:完整示例的下载 URL 现在在 the documentation page 的顶部更加突出。]
- 将完整的示例添加到页面,这样您就可以直接在页面上查看和复制它,而不必下载 zip 文件。 [更新: 完整示例现在是 published at the bottom of the documentation page。]