首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用的流式数据

使用的流式数据
EN

Stack Overflow用户
提问于 2014-02-26 19:46:55
回答 1查看 1.8K关注 0票数 1

到目前为止,我一直遵循这些步骤。

为web/http设置cloudfront发行版并启用它。

然后生成我的CloudFront密钥对,保存文件并上传到我的服务器。

我有我的Amazon所有的设置来生成getSignedUrl,并且有下面的代码。

代码语言:javascript
复制
<?php
use Aws\CloudFront\CloudFrontClient;

$client = CloudFrontClient::factory(array(
    'private_key'    => dirname(__FILE__) . '/cloudfront/pk- my key here -.pem',
    'key_pair_id' => 'AWS Key'
));

$distributionUrl = '- my url here -.cloudfront.net';

$url = $client->getSignedUrl(array(
    'url'     => 'https://' . $distributionUrl . '/video.mp4',
    'expires' => time() + 3600
));

?>

<video width="320" height="240" controls>
  <source src="<?php echo $url; ?>" type="video/mp4">
  Your browser does not support the video tag.
</video>

好的,所以它正在生成一个url,但是它不会在视频播放器中播放,如果我在chrome中查看控制台,它会显示这个错误。

更新我知道有能力直接从cloudfront流,只要我的文件是公开的,我不能让getSignedUrl工作在如此令人沮丧。

有人能帮上忙吗?这件事已经持续了几个小时了?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-03-14 00:32:15

CloudFront found 这里的文档给出了一个如何使用cloudfront为可流私有内容服务的示例。

使用PHP创建URL签名 任何运行PHP的web服务器都可以使用PHP演示代码为私有CloudFront RTMP发行版创建策略语句和签名。该示例创建一个功能良好的网页,其中包含有签名的URL链接,该链接使用CloudFront流播放视频流。要获得示例,请下载PHP中视频流的签名代码

参考见这个问题

代码语言:javascript
复制
/**
 * Create a signed URL
 *
 * This method accepts an array of configuration options:
 * - url:       (string)  URL of the resource being signed (can include query string and wildcards). For example:
 *                        rtmp://s5c39gqb8ow64r.cloudfront.net/videos/mp3_name.mp3
 *                        http://d111111abcdef8.cloudfront.net/images/horizon.jpg?size=large&license=yes
 * - policy:    (string)  JSON policy. Use this option when creating a signed URL for a custom policy.
 * - expires:   (int)     UTC Unix timestamp used when signing with a canned policy. Not required when passing a
 *                        custom 'policy' option.
 *
 * @param array $options Array of configuration options used when signing
 *
 * @return string                   The file URL with authentication parameters.
 * @throws InvalidArgumentException if key_pair_id and private_key have not been configured on the client
 */
public function getSignedUrl(array $options)
{
    if (!$this->getConfig('key_pair_id') || !$this->getConfig('private_key')) {
        throw new InvalidArgumentException(
            'An Amazon CloudFront keypair ID (key_pair_id) and an RSA private key (private_key) is required'
        );
    }

    // Initialize the configuration data and ensure that the url was specified
    $options = Collection::fromConfig($options, null, array('url'));
    // Determine the scheme of the policy
    $urlSections = explode('://', $options['url']);
    // Ensure that the URL contained a scheme and parts after the scheme
    if (count($urlSections) < 2) {
        throw new InvalidArgumentException('Invalid URL: ' . $options['url']);
    }

    // Get the real scheme by removing wildcards from the scheme
    $scheme = str_replace('*', '', $urlSections[0]);
    $policy = $options['policy'] ?: $this->createCannedPolicy($scheme, $options['url'], $options['expires']);
    // Strip whitespace from the policy
    $policy = str_replace(' ', '', $policy);

    $url = Url::factory($scheme . '://' . $urlSections[1]);
    if ($options['policy']) {
        // Custom policies require that the encoded policy be specified in the URL
        $url->getQuery()->set('Policy', strtr(base64_encode($policy), '+=/', '-_~'));
    } else {
        // Canned policies require that the Expires parameter be set in the URL
        $url->getQuery()->set('Expires', $options['expires']);
    }

    // Sign the policy using the CloudFront private key
    $signedPolicy = $this->rsaSha1Sign($policy, $this->getConfig('private_key'));
    // Remove whitespace, base64 encode the policy, and replace special characters
    $signedPolicy = strtr(base64_encode($signedPolicy), '+=/', '-_~');

    $url->getQuery()
        ->useUrlEncoding(false)
        ->set('Signature', $signedPolicy)
        ->set('Key-Pair-Id', $this->getConfig('key_pair_id'));

    if ($scheme != 'rtmp') {
        // HTTP and HTTPS signed URLs include the full URL
        return (string) $url;
    } else {
        // Use a relative URL when creating Flash player URLs
        $url->setScheme(null)->setHost(null);
        // Encode query string variables for flash players
        $url = str_replace(array('?', '=', '&'), array('%3F', '%3D', '%26'), (string) $url);

        return substr($url, 1);
    }
}

/**
 * Sign a policy string using OpenSSL RSA SHA1
 *
 * @param string $policy             Policy to sign
 * @param string $privateKeyFilename File containing the OpenSSL private key
 *
 * @return string
 */
protected function rsaSha1Sign($policy, $privateKeyFilename)
{
    $signature = '';
    openssl_sign($policy, $signature, file_get_contents($privateKeyFilename));

    return $signature;
}

/**
 * Create a canned policy for a particular URL and expiration
 *
 * @param string $scheme  Parsed scheme without wildcards
 * @param string $url     URL that is being signed
 * @param int    $expires Time in which the signature expires
 *
 * @return string
 * @throws InvalidArgumentException if the expiration is not set
 */
protected function createCannedPolicy($scheme, $url, $expires)
{
    if (!$expires) {
        throw new InvalidArgumentException('An expires option is required when using a canned policy');
    }

    // Generate a canned policy
    if ($scheme == 'http' || $scheme == 'https') {
        $resource = $url;
    } elseif ($scheme == 'rtmp') {
        $parts = parse_url($url);
        $pathParts = pathinfo($parts['path']);
        // Add path leading to file, strip file extension, and add a query string if present
        $resource = ltrim($pathParts['dirname'] . '/' . $pathParts['filename'], '/')
            . (isset($parts['query']) ? "?{$parts['query']}" : '');
    } else {
        throw new InvalidArgumentException("Invalid URI scheme: {$scheme}. Must be one of http or rtmp.");
    }

    return sprintf(
        '{"Statement":[{"Resource":"%s","Condition":{"DateLessThan":{"AWS:EpochTime":%d}}}]}',
        $resource,
        $expires
    );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22051701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档