如何使用REST API将视频上传到Azure媒体服务?

nmpmafwu  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(133)

首先,我通过下面的代码得到了访问令牌

$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $tokenEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'resource' => 'https://management.azure.com'
);
$requestBody = http_build_query($data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);

$responseData = json_decode($response, true);
if (isset($responseData['access_token'])) {
   echo $accessToken = $responseData['access_token'];

} else {
    echo "Failed to obtain access token.";
}

然后POST REQUEST Via REST api并传递此访问令牌,代码如下:

$subscriptionId = "c219b0b7-cc1d-4cdb-b323-xxxxxxxxxx";
$resourceGroupName = "maxtv2";
$accountName = "maxtv2";
$assetName = "Plan-Details"; 
 
$url = "https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Media/mediaServices/{$accountName}/assets/{$assetName}/files?api-version=2023-01-01"; 

$filePath = "./sample.mp4";

$boundary = uniqid();

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "Authorization: Bearer $accessToken",
    "Content-Type: multipart/form-data; boundary=$boundary"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$fileContents = file_get_contents($filePath);
$requestBody = "--$boundary\r\n";
$requestBody .= "Content-Disposition: form-data; name=\"file\"; filename=\"" . basename($filePath) . "\"\r\n";
$requestBody .= "Content-Type: video/mp4\r\n\r\n";
$requestBody .= $fileContents . "\r\n";
$requestBody .= "--$boundary--";

curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);

$response = curl_exec($ch);

if(curl_errno($ch)){
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);

echo 'Response: '.$response;

但得到的响应是:{"error ":{" code":"GatewayTimeout","message":"网关在指定的时间段内未收到来自'Microsoft. Media'的响应。"}}
如何解决这个问题??
文件上传成功消息

cgyqldqp

cgyqldqp1#

我已经与Azure媒体服务内部团队就此进行了检查,我们没有任何直接的默认REST API来将文件上传到媒体服务中的资产。
在您的脚本中,您需要使用create or update Asset REST API创建AMS资产,然后您需要使用Azure存储API(Putblob)将文件上传到存储帐户。
或者,您可以使用下面的PowerShell脚本,其中我们使用创建应用程序注册的访问令牌分配必要的权限(媒体服务媒体操作员RBAC)来访问媒体服务,然后通过上述REST API创建资产,进一步通过PowerShell cmdlet将视频上传到AMS资产。

$TenantId = "<tenantId>"
$clientId= "<ClientId>"
$clientSecret= "<ClientSecret>"

$subscriptionId= "<SubscriptionId>"
$RgroupName="<ResourceGroupName>"
$strgaccountname="<StorageAccountName>
$AMSAccountName = "<MediaServiceAccoutName>"
$assetName= "<AssetName>"
$blobName="<BlobName>

## Access token generation

$RequestAccessTokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$audience = "https://management.core.windows.net/"

$body = "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret&resource=$audience"

$Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'

##assest creation 

$createassetrequesturi= 'https://management.azure.com/subscriptions/'+ $subscriptionId + '/resourcegroups/' + $RgroupName + '/providers/Microsoft.Media/mediaservices/' + $AMSAccountName + '/assets/' + $assetName + '?api-version=2022-08-01'
$body= '{"properties": {"storageAccountName": "<StorageAccountName>"}}'

$assetdetails=Invoke-RestMethod -Method Put -Uri $createassetrequesturi -Headers @{'authorization'= "Bearer " + $Token.access_token } -Body $body -ContentType "application/json"

##SAS token creation and video upload to asset

$expiryTime = (Get-Date).AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$permissions = "rwdl"
$containerName= $assetdetails.properties.container

$keys=(Get-AzStorageAccountKey -ResourceGroupName $RgroupName -AccountName $strgaccountname ).Value[0]
$context=New-AzStorageContext -StorageAccountName $strgaccountname -StorageAccountKey $keys
$sastoken = New-AzStorageContainerSASToken -Context $context -Container $containerName -ExpiryTime $expiryTime -Permission "rwdl"

Set-AzStorageBlobContent -Container $containerName -Blob $blobname -File <localMachinepath> -Context $context

相关问题