简体   繁体   English

通过Javascript和PHP组合将文件上传到Azure Media Services资产

[英]Upload a file to Azure Media Services asset via Javascript and PHP combo

I'm looking into enabling my users to upload own video files. 我正在考虑让我的用户上传自己的视频文件。 For this, I'd like to use Azure Media Services. 为此,我想使用Azure媒体服务。 As users will be uploading directly from their device to Azure, the only route seems to be using Shared Access Signatures. 由于用户将直接从其设备上载到Azure,因此唯一的途径似乎是使用共享访问签名。

As seen otherwise , the common flow is: 否则 ,常见流程为:

  1. client requests the URL to upload to via a web API 客户请求通过Web API上传的URL
  2. web API will create an asset via AMS SDK (which is a Storage container) Web API将通过AMS SDK(这是一个存储容器)创建资产
  3. web API will create an write-only SAS URL for that container Web API将为该容器创建一个只读SAS URL
  4. web API will return the SAS URL Web API将返回SAS URL
  5. client does a new request directly to that URL 客户直接向该URL发出新请求

This all seems good, but most/all examples of doing this are in C# and I need it in PHP. 这一切看起来都不错,但是大多数/所有执行此操作的示例都在C#中,而我在PHP中需要它。 The official SDK closed the issue which asked for the support, there's another one which is open for a year. 官方SDK解决了要求获得支持的问题还有一个开放了一年的问题。

I've created a SAS link (using beberlei/azure-blob-storage ) 我已经创建了SAS链接(使用beberlei / azure-blob-storage

https://media[redacted].blob.core.windows.net/asset-adc73a5d-1500-80c5-173d-f1e5d00fd8b2?st=2016-02-10T15%3A58%3A44.0000000Z&se=2016-02-10T16%3A33%3A44.0000000Z&sr=c&sp=w&sig=O9p6jyAoYltWwUHughaC9g3mBacdBnZEbuBDahPfCFA%3D https:// media [redacted..blob.core.windows.net / asset-adc73a5d-1500-80c5-173d-f1e5d00fd8b2?st = 2016-02-10T15%3A58%3A44.0000000Z&se = 2016-02-10T16%3A33 %3A44.0000000Z&sr = c&sp = w&sig = O9p6jyAoYltWwUHughaC9g3mBacdBnZEbuBDahPfCFA%3D

but whatever I do, I'm always getting a message: 但是无论我做什么,我总是收到一条消息:

Signature did not match. 签名不匹配。 String to sign used was w 2016-02-10T15:58:44.0000000Z 2016-02-10T16:33:44.0000000Z /media[redacted]/asset-adc73a5d-1500-80c5-173d-f1e5d00fd8b2 用于签名的字符串为w 2016-02-10T15:58:44.0000000Z 2016-02-10T16:33:44.0000000Z / media [redacted] / asset-adc73a5d-1500-80c5-173d-f1e5d00fd8b2

So: 所以:

  1. is my understanding of the proper workflow correct here? 我对正确的工作流程的理解在这里正确吗? Do I create the asset first, then create a SAS token on the asset as if it's a plain blob container? 我是否先创建资产,然后在资产上创建一个SAS令牌,就好像它是普通的Blob容器一样?
  2. is there an up-to-date PHP library / SDK which I can use to do this? 有没有最新的PHP库/ SDK,可以用来执行此操作?

As I was typing this question, I came across the answer, hope it helps somebody else so I won't delete it. 当我键入此问题时,我遇到了答案,希望它对其他人有帮助,所以我不会将其删除。

The reason it failed because I was using my Media services account name / key for both access to AMS API and for SAS generation. 失败的原因是因为我使用我的媒体服务帐户名/密钥访问AMS API和生成SAS。 This is wrong. 错了 I needed to: 我需要:

  1. use AMS account / key for creating the asset 使用AMS帐户/密钥创建资产
  2. use linked storage account / key for creating the SAS URL 使用链接的存储帐户/密钥创建SAS URL

I'm getting a CORS-related error here which seems like a step in the right direction, will update if it failed. 我在这里遇到与CORS相关的错误,这似乎是朝着正确方向迈出的一步,如果失败,它将更新。

Edit 1 : it did in fact fail. 编辑1 :它实际上确实失败了。 I needed to update my API calls version to 2013-08-13 and up, this changed the proper SAS checksum generation. 我需要将我的API调用版本更新到2013-08-13及更高版本,这改变了正确的SAS校验和生成。 Note that the current official PHP SDK will only use storage version 2012-02-12 so you'll be unable to enable CORS with it. 请注意, 当前的官方PHP SDK仅将使用存储版本2012-02-12,因此您将无法启用它的CORS。

My code is as follows (it's super-messy as I'm doing this as a prototype): 我的代码如下(当我将其作为原型时,这太混乱了):

<?php
use Beberlei\AzureBlobStorage\SharedAccessSignature;
use WindowsAzure\Common\Internal\MediaServicesSettings;
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\MediaServices\MediaServicesRestProxy;
use WindowsAzure\MediaServices\Models\Asset;

require 'vendor/autoload.php';

$fileName = $_GET['blobName'];
$accountName = '<AMS account>';
$accessKey = '<AMS key>';
$settings = new MediaServicesSettings($accountName, $accessKey);

/** @var MediaServicesRestProxy $proxy */
$proxy = ServicesBuilder::getInstance()->createMediaServicesService($settings);
$asset = new Asset(Asset::OPTIONS_NONE);
$asset->setName($fileName);

/** @var Asset $asset */
$asset = $proxy->createAsset($asset);
$assetId = $asset->getId();
$path = parse_url($asset->getUri(), PHP_URL_PATH);

$startTime = new \DateTime('now -5 minutes');
$expiryTime = new \DateTime('now +30 minutes');
$signatureGenerator = new SharedAccessSignature('<storage account>', '<storage key>');
$signature = $signatureGenerator->createSignedQueryString(
    $path,
    '',
    'c',
    'w',
    isoDate($startTime),
    isoDate($expiryTime)
);

die($asset->getUri().'?'.$signature);

function isoDate(\DateTime $dateTime)
{
    $tz = date_default_timezone_get();
    date_default_timezone_set('UTC');
    $returnValue = str_replace('+00:00', '.0000000Z', date('c', $dateTime->getTimestamp()));
    date_default_timezone_set($tz);

    return $returnValue;
}

If you are uploding video file via JavaScript, you need to set CORS on the storage account under your media services account. 如果要通过JavaScript上载视频文件,则需要在媒体服务帐户下的存储帐户上设置CORS。 Please refer to this documentation to set CORS setting on your blob storage (enable your site to write to storage): https://msdn.microsoft.com/en-us/library/azure/dn535601.aspx . 请参考此文档以在Blob存储上设置CORS设置(使您的站点能够写入存储): https : //msdn.microsoft.com/zh-cn/library/azure/dn535601.aspx I believe there are some handy tools allow you to that also without writing code. 我相信有一些方便的工具也可以让您无需编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM