Reputation: 21
Firstly, I got the access token using below code
$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.";
}
and then POST REQUEST Via REST api and passed this access token, the code is below:
$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;
but getting the Response: {"error":{"code":"GatewayTimeout","message":"The gateway did not receive a response from 'Microsoft.Media' within the specified time period."}}
how to solve this??
the file uploading success message
Upvotes: 0
Views: 232
Reputation: 5506
I have checked with the Azure Media service internal team on this, We don't have any direct default REST API to upload a file to assets in Media service.
In your script, You need to use the create or update Asset REST API to create AMS asset and then you need to use Azure storage API ( Putblob) to upload a file to storage account.
Alternatively, you can use the below PowerShell script in which we are using creating an access token to the app registration Assigned with necessary permissions(Media Services media operator RBAC) to access media service and then creating the assets through above mentioned rest API further uploading the video to AMS asset through PowerShell cmdlets.
$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
Upvotes: 0