首先,我通过下面的代码得到了访问令牌
$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'的响应。"}}
如何解决这个问题??
文件上传成功消息
1条答案
按热度按时间cgyqldqp1#
我已经与Azure媒体服务内部团队就此进行了检查,我们没有任何直接的默认REST API来将文件上传到媒体服务中的资产。
在您的脚本中,您需要使用create or update Asset REST API创建AMS资产,然后您需要使用Azure存储API(Putblob)将文件上传到存储帐户。
或者,您可以使用下面的PowerShell脚本,其中我们使用创建应用程序注册的访问令牌分配必要的权限(媒体服务媒体操作员RBAC)来访问媒体服务,然后通过上述REST API创建资产,进一步通过PowerShell cmdlet将视频上传到AMS资产。