public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info("Request : " + jsonContent); // Validate input objects int delay = 5000; if (data.targetStorageAccountName == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetStorageAccountName in the input object" })); } if (data.targetStorageAccountKey == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetStorageAccountKey in the input object" })); } if (data.targetContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetContainer in the input object" })); } if (data.delay != null) { delay = data.delay; } log.Info("Input - DestinationContainer : " + data.targetContainer); //log.Info("delay : " + delay); log.Info($"Wait " + delay + "(ms)"); System.Threading.Thread.Sleep(delay); string targetStorageAccountName = data.targetStorageAccountName; string targetStorageAccountKey = data.targetStorageAccountKey; string targetContainer = data.targetContainer; CopyStatus copyStatus = CopyStatus.Success; try { CloudBlobContainer destinationBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(targetStorageAccountName, targetStorageAccountKey, targetContainer); string blobPrefix = null; if (!string.IsNullOrWhiteSpace((string)data.fileNamePrefix)) { blobPrefix = (string)data.fileNamePrefix; log.Info($"{blobPrefix}"); } bool useFlatBlobListing = true; var destBlobList = destinationBlobContainer.ListBlobs(blobPrefix, useFlatBlobListing, BlobListingDetails.Copy); foreach (var dest in destBlobList) { var destBlob = dest as CloudBlob; if (destBlob.CopyState.Status == CopyStatus.Aborted || destBlob.CopyState.Status == CopyStatus.Failed) { // Log the copy status description for diagnostics and restart copy destBlob.StartCopyAsync(destBlob.CopyState.Source); copyStatus = CopyStatus.Pending; } else if (destBlob.CopyState.Status == CopyStatus.Pending) { // We need to continue waiting for this pending copy // However, let us log copy state for diagnostics copyStatus = CopyStatus.Pending; } // else we completed this pending copy } } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } return(req.CreateResponse(HttpStatusCode.OK, new { copyStatus = copyStatus, isRunning = (copyStatus == CopyStatus.Pending).ToString(), isSuccessful = (copyStatus == CopyStatus.Success).ToString() })); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info("Request : " + jsonContent); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); // Validate input objects if (data.assetId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" })); } if (data.targetStorageAccountName == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetStorageAccountName in the input object" })); } if (data.targetStorageAccountKey == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetStorageAccountKey in the input object" })); } if (data.targetContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass targetContainer in the input object" })); } string targetStorageAccountName = data.targetStorageAccountName; string targetStorageAccountKey = data.targetStorageAccountKey; string targetContainer = data.targetContainer; string startsWith = data.startsWith; string endsWith = data.endsWith; log.Info("Input - targetStorageAccountName : " + targetStorageAccountName); log.Info("Input - targetStorageAccountKey : " + targetStorageAccountKey); log.Info("Input - targetContainer : " + targetContainer); string assetId = data.assetId; IAsset asset = null; IIngestManifest manifest = null; MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}"); try { AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // Find the Asset asset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault(); if (asset == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (asset.StorageAccountName != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(asset.StorageAccountName)) // asset is using another storage than default but we have the key { storname = asset.StorageAccountName; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"Face redaction Asset is in {asset.StorageAccountName} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" })); } } // Setup blob container CloudBlobContainer sourceBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, asset.Uri.Segments[1]); CloudBlobContainer destinationBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(targetStorageAccountName, targetStorageAccountKey, targetContainer); destinationBlobContainer.CreateIfNotExists(); var files = asset.AssetFiles.ToList().Where(f => ((string.IsNullOrEmpty(endsWith) || f.Name.EndsWith(endsWith)) && (string.IsNullOrEmpty(startsWith) || f.Name.StartsWith(startsWith)))); foreach (var file in files) { CloudBlob sourceBlob = sourceBlobContainer.GetBlockBlobReference(file.Name); CloudBlob destinationBlob = destinationBlobContainer.GetBlockBlobReference(file.Name); CopyBlobHelpers.CopyBlobAsync(sourceBlob, destinationBlob); log.Info($"Start copy of file : {file.Name}"); } } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } return(req.CreateResponse(HttpStatusCode.OK)); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { { log.Info($"Webhook was triggered!"); // Init variables string pathUrl = ""; dynamic pngThumbnails = new JArray() as dynamic; string prefixpng = ""; string copyToContainer = ""; string targetContainerUri = ""; TimeSpan timeOffset = new TimeSpan(0); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); log.Info(jsonContent); MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}"); try { AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // // MES Thumbnails // if (data.mesThumbnails != null && data.mesThumbnails.assetId != null) { List <CloudBlob> listPNGCopies = new List <CloudBlob>(); // Get the asset string assetid = data.mesThumbnails.assetId; var outputAsset = _context.Assets.Where(a => a.Id == assetid).FirstOrDefault(); if (outputAsset == null) { log.Info($"Asset not found {assetid}"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } var pngFiles = outputAsset.AssetFiles.Where(a => a.Name.ToUpper().EndsWith(".PNG")); Uri publishurl = MediaServicesHelper.GetValidOnDemandPath(_context, outputAsset); if (publishurl != null) { pathUrl = publishurl.ToString(); } else { log.Info($"Asset not published"); } // Let's copy the PNG Thumbnails if (data.mesThumbnails.copyToContainer != null) { copyToContainer = data.mesThumbnails.copyToContainer + DateTime.UtcNow.ToString("yyyyMMdd"); // let's copy PNG to a container prefixpng = outputAsset.Uri.Segments[1] + "-"; log.Info($"prefixpng {prefixpng}"); string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (outputAsset.StorageAccountName != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(outputAsset.StorageAccountName)) // asset is using another storage than default but we have the key { storname = outputAsset.StorageAccountName; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"MES Thumbnails Asset is in {outputAsset.StorageAccountName} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" })); } } var sourceContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, outputAsset.Uri.Segments[1]); CloudBlobContainer targetContainer; if (data.mesThumbnails.copyToContainerAccountName != null) { // copy to a specific storage account targetContainer = CopyBlobHelpers.GetCloudBlobContainer((string)data.mesThumbnails.copyToContainerAccountName, (string)data.mesThumbnails.copyToContainerAccountKey, copyToContainer); } else { // copy to ams storage account targetContainer = CopyBlobHelpers.GetCloudBlobContainer(amsCredentials.StorageAccountName, amsCredentials.StorageAccountKey, copyToContainer); } listPNGCopies = await CopyBlobHelpers.CopyFilesAsync(sourceContainer, targetContainer, prefixpng, "png", log); targetContainerUri = targetContainer.Uri.ToString(); } foreach (IAssetFile file in pngFiles) { string index = file.Name.Substring(file.Name.Length - 10, 6); int index_i = 0; if (int.TryParse(index, out index_i)) { dynamic entry = new JObject(); entry.id = index_i; entry.fileId = file.Id; entry.fileName = file.Name; if (copyToContainer != "") { entry.url = targetContainerUri + "/" + prefixpng + file.Name; } else if (!string.IsNullOrEmpty(pathUrl)) { entry.url = pathUrl + file.Name; } pngThumbnails.Add(entry); } } if (data.mesThumbnails.deleteAsset != null && ((bool)data.mesThumbnails.deleteAsset)) { // If asset deletion was asked // let's wait for the copy to finish before deleting the asset.. if (listPNGCopies.Count > 0) { log.Info("PNG Copy with asset deletion was asked. Checking copy status..."); bool continueLoop = true; while (continueLoop) { listPNGCopies = listPNGCopies.Where(r => r.CopyState.Status == CopyStatus.Pending).ToList(); if (listPNGCopies.Count == 0) { continueLoop = false; } else { log.Info("PNG Copy not finished. Waiting 3s..."); Task.Delay(TimeSpan.FromSeconds(3d)).Wait(); listPNGCopies.ForEach(r => r.FetchAttributes()); } } } outputAsset.Delete(); } } } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } log.Info($""); return(req.CreateResponse(HttpStatusCode.OK, new { mesThumbnail = new { pngThumbnails = Newtonsoft.Json.JsonConvert.SerializeObject(pngThumbnails) } })); } }
public static async Task<object> Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info("Request : " + jsonContent); MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); // Validate input objects int delay = 5000; if (data.destinationContainer == null) return req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass DestinationContainer in the input object" }); if (data.delay != null) delay = data.delay; log.Info("Input - DestinationContainer : " + data.destinationContainer); //log.Info("delay : " + delay); log.Info($"Wait " + delay + "(ms)"); System.Threading.Thread.Sleep(delay); string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (data.assetStorage != null) { string assetstor = (string)data.assetStorage; if (assetstor != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(assetstor)) // asset is using another storage than default but we have the key { storname = assetstor; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"Face redaction Asset is in {assetstor} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" }); } } } string destinationContainerName = data.destinationContainer; CloudBlobContainer destinationBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, destinationContainerName); CopyStatus copyStatus = CopyStatus.Success; try { string blobPrefix = null; bool useFlatBlobListing = true; var destBlobList = destinationBlobContainer.ListBlobs(blobPrefix, useFlatBlobListing, BlobListingDetails.Copy); foreach (var dest in destBlobList) { var destBlob = dest as CloudBlob; if (destBlob.CopyState.Status == CopyStatus.Aborted || destBlob.CopyState.Status == CopyStatus.Failed) { // Log the copy status description for diagnostics and restart copy destBlob.StartCopyAsync(destBlob.CopyState.Source); copyStatus = CopyStatus.Pending; } else if (destBlob.CopyState.Status == CopyStatus.Pending) { // We need to continue waiting for this pending copy // However, let us log copy state for diagnostics copyStatus = CopyStatus.Pending; } // else we completed this pending copy } } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message }); } return req.CreateResponse(HttpStatusCode.OK, new { copyStatus = copyStatus, isRunning = (copyStatus == CopyStatus.Pending).ToString(), isSuccessful = (copyStatus == CopyStatus.Success).ToString() }); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info(jsonContent); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); if (data.assetId == null) { // for test // data.Path = "/input/WP_20121015_081924Z.mp4"; return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" })); } MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}"); try { AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // Step 1: Copy the Blob into a new Input Asset for the Job // ***NOTE: Ideally we would have a method to ingest a Blob directly here somehow. // using code from this sample - https://azure.microsoft.com/en-us/documentation/articles/media-services-copying-existing-blob/ // Get the asset string assetid = data.assetId; var asset = _context.Assets.Where(a => a.Id == assetid).FirstOrDefault(); if (asset == null) { log.Info($"Asset not found {assetid}"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } log.Info("Asset found, ID: " + asset.Id); string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (asset.StorageAccountName != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(asset.StorageAccountName)) // asset is using another storage than default but we have the key { storname = asset.StorageAccountName; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"Face redaction Asset is in {asset.StorageAccountName} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" })); } } CloudBlobContainer assetContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, asset.Uri.Segments[1]); foreach (String seg in asset.Uri.Segments) { log.Info($"> asset.Uri.Segments :" + seg); } /*IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers(); * foreach (CloudBlobContainer item in containers) * { * foreach (IListBlobItem blob in item.ListBlobs()) * { * blobs.Add(string.Format("{0}", blob.Uri.Segments[2])); * } * } */ /* * //Get a reference to the storage account that is associated with the Media Services account. * StorageCredentials mediaServicesStorageCredentials = * new StorageCredentials(_storageAccountName, _storageAccountKey); * var _destinationStorageAccount = new CloudStorageAccount(mediaServicesStorageCredentials, false); * * CloudBlobClient destBlobStorage = _destinationStorageAccount.CreateCloudBlobClient(); * * // Get the destination asset container reference * string destinationContainerName = asset.Uri.Segments[1]; * log.Info($"destinationContainerName : {destinationContainerName}"); * * CloudBlobContainer assetContainer = destBlobStorage.GetContainerReference(destinationContainerName); */ log.Info($"assetContainer retrieved"); // Get hold of the destination blobs var blobsPseudo = assetContainer.ListBlobs(); log.Info($"blobsPseudo retrieved"); log.Info($"blobsPseudo count : {blobsPseudo.Count()}"); var aflist = asset.AssetFiles.ToList().Select(af => af.Name); /* OLD WAY * foreach (CloudBlockBlob blob in blobs) * { * if (aflist.Contains(blob.Name)) * { * var assetFile = asset.AssetFiles.Where(af => af.Name == blob.Name).FirstOrDefault(); * assetFile.ContentFileSize = blob.Properties.Length; * assetFile.Update(); * log.Info($"Asset file updated : {assetFile.Name}"); * } * else * { * var assetFile = asset.AssetFiles.Create(blob.Name); * assetFile.ContentFileSize = blob.Properties.Length; * assetFile.Update(); * log.Info($"Asset file created : {assetFile.Name}"); * } * } */ log.Info($"Witness 3 "); //Adding mecanic to list all sub dir (original just lists elements in given folder and consider them as blob even if they are subdirs) //var folders = blobs.Where(b => b as CloudBlobDirectory != null).ToList(); //this is supposedly a 2 levels scenario... might not suits our case yet (one more level?) foreach (IListBlobItem blobItem in blobsPseudo) { log.Info($"blobItem found : "); if (blobItem is CloudBlobDirectory) { CloudBlobDirectory directory = (CloudBlobDirectory)blobItem; IEnumerable <IListBlobItem> blobs = directory.ListBlobs(); //ICloudBlob bi; foreach (var item in blobs) { log.Info($"item StorageUri : " + item.StorageUri); CloudBlockBlob blob = (CloudBlockBlob)item; blob.FetchAttributes(); log.Info($"Blob found uri : " + blob.Uri); log.Info($"Blob found Name : " + blob.Name); String potentialName = Path.GetFileName(blob.Name); log.Info($"Blob potentialName : " + potentialName); String normalizedName = potentialName; if (aflist.Contains(normalizedName)) { log.Info($" aflist.Contains Blob found Name : " + normalizedName); var assetFile = asset.AssetFiles.Where(af => af.Name == normalizedName).FirstOrDefault(); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset file updated : {normalizedName}"); } else { log.Info($"Create Blob found Name : " + normalizedName); var assetFile = asset.AssetFiles.Create(normalizedName); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset file created : {normalizedName}"); } } } else { //TODO redefine ! CloudBlockBlob blob = (CloudBlockBlob)blobItem; blob.FetchAttributes(); log.Info($"Blob2 found uri : " + blob.Uri); log.Info($"Blob2 found Name : " + blob.Name); if (aflist.Contains(blob.Name)) { var assetFile = asset.AssetFiles.Where(af => af.Name == blob.Name).FirstOrDefault(); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset2 file updated : {assetFile.Name}"); } else { var assetFile = asset.AssetFiles.Create(blob.Name); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset2 file created : {assetFile.Name}"); } } } asset.Update(); MediaServicesHelper.SetAFileAsPrimary(asset); log.Info("Asset updated"); } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } return(req.CreateResponse(HttpStatusCode.OK)); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info(jsonContent); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); if (data.assetId == null) { // for test // data.Path = "/input/WP_20121015_081924Z.mp4"; return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" })); } MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}"); try { AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // Step 1: Copy the Blob into a new Input Asset for the Job // ***NOTE: Ideally we would have a method to ingest a Blob directly here somehow. // using code from this sample - https://azure.microsoft.com/en-us/documentation/articles/media-services-copying-existing-blob/ // Get the asset string assetid = data.assetId; var asset = _context.Assets.Where(a => a.Id == assetid).FirstOrDefault(); if (asset == null) { log.Info($"Asset not found {assetid}"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } log.Info("Asset found, ID: " + asset.Id); string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (asset.StorageAccountName != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(asset.StorageAccountName)) // asset is using another storage than default but we have the key { storname = asset.StorageAccountName; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"Face redaction Asset is in {asset.StorageAccountName} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" })); } } CloudBlobContainer assetContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, asset.Uri.Segments[1]); /* * //Get a reference to the storage account that is associated with the Media Services account. * StorageCredentials mediaServicesStorageCredentials = * new StorageCredentials(_storageAccountName, _storageAccountKey); * var _destinationStorageAccount = new CloudStorageAccount(mediaServicesStorageCredentials, false); * * CloudBlobClient destBlobStorage = _destinationStorageAccount.CreateCloudBlobClient(); * * // Get the destination asset container reference * string destinationContainerName = asset.Uri.Segments[1]; * log.Info($"destinationContainerName : {destinationContainerName}"); * * CloudBlobContainer assetContainer = destBlobStorage.GetContainerReference(destinationContainerName); */ log.Info($"assetContainer retrieved"); // Get hold of the destination blobs var blobs = assetContainer.ListBlobs(); log.Info($"blobs retrieved"); log.Info($"blobs count : {blobs.Count()}"); var aflist = asset.AssetFiles.ToList().Select(af => af.Name); foreach (CloudBlockBlob blob in blobs) { if (aflist.Contains(blob.Name)) { var assetFile = asset.AssetFiles.Where(af => af.Name == blob.Name).FirstOrDefault(); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset file updated : {assetFile.Name}"); } else { var assetFile = asset.AssetFiles.Create(blob.Name); assetFile.ContentFileSize = blob.Properties.Length; assetFile.Update(); log.Info($"Asset file created : {assetFile.Name}"); } } asset.Update(); MediaServicesHelper.SetAFileAsPrimary(asset); log.Info("Asset updated"); } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } return(req.CreateResponse(HttpStatusCode.OK)); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"Webhook was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Info("Request : " + jsonContent); var attachedstoragecred = KeyHelper.ReturnStorageCredentials(); // Validate input objects if (data.assetId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" })); } if (data.fileName == null && data.fileNames == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass fileName or fileNames in the input object" })); } if (data.sourceStorageAccountName == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass sourceStorageAccountName in the input object" })); } if (data.sourceStorageAccountKey == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass sourceStorageAccountKey in the input object" })); } if (data.sourceContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass sourceContainer in the input object" })); } log.Info("Input - sourceStorageAccountName : " + data.sourceStorageAccountName); log.Info("Input - sourceStorageAccountKey : " + data.sourceStorageAccountKey); log.Info("Input - sourceContainer : " + data.sourceContainer); string _sourceStorageAccountName = data.sourceStorageAccountName; string _sourceStorageAccountKey = data.sourceStorageAccountKey; string assetId = data.assetId; bool missingBlob = false; IAsset newAsset = null; IIngestManifest manifest = null; MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}"); try { AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // Find the Asset newAsset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault(); if (newAsset == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } // Setup blob container CloudBlobContainer sourceBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(_sourceStorageAccountName, _sourceStorageAccountKey, (string)data.sourceContainer); string storname = amsCredentials.StorageAccountName; string storkey = amsCredentials.StorageAccountKey; if (newAsset.StorageAccountName != amsCredentials.StorageAccountName) { if (attachedstoragecred.ContainsKey(newAsset.StorageAccountName)) // asset is using another storage than default but we have the key { storname = newAsset.StorageAccountName; storkey = attachedstoragecred[storname]; } else // we don't have the key for that storage { log.Info($"Face redaction Asset is in {newAsset.StorageAccountName} and key is not provided in MediaServicesAttachedStorageCredentials application settings"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Storage key is missing" })); } } CloudBlobContainer destinationBlobContainer = CopyBlobHelpers.GetCloudBlobContainer(storname, storkey, newAsset.Uri.Segments[1]); sourceBlobContainer.CreateIfNotExists(); if (data.fileName != null) { string fileName = (string)data.fileName; CloudBlob sourceBlob = sourceBlobContainer.GetBlockBlobReference(fileName); if (data.wait != null && (bool)data.wait) { for (int i = 1; i <= 3; i++) // let's wait 3 times 5 seconds (15 seconds) { if (sourceBlob.Exists()) { break; } log.Info("Waiting 5 s..."); System.Threading.Thread.Sleep(5 * 1000); sourceBlob = sourceBlobContainer.GetBlockBlobReference(fileName); } } if (sourceBlob.Exists()) { CloudBlob destinationBlob = destinationBlobContainer.GetBlockBlobReference(fileName); if (destinationBlobContainer.CreateIfNotExists()) { log.Info("container created"); destinationBlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); } CopyBlobHelpers.CopyBlobAsync(sourceBlob, destinationBlob); } else { missingBlob = true; } } if (data.fileNames != null) { foreach (var file in data.fileNames) { string fileName = (string)file; CloudBlob sourceBlob = sourceBlobContainer.GetBlockBlobReference(fileName); if (sourceBlob.Exists()) { CloudBlob destinationBlob = destinationBlobContainer.GetBlockBlobReference(fileName); if (destinationBlobContainer.CreateIfNotExists()) { log.Info("container created"); destinationBlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); } CopyBlobHelpers.CopyBlobAsync(sourceBlob, destinationBlob); } else { missingBlob = true; } } } } catch (Exception ex) { string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : ""); log.Info($"ERROR: Exception {message}"); return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message })); } return(req.CreateResponse(HttpStatusCode.OK, new { destinationContainer = newAsset.Uri.Segments[1], missingBlob = missingBlob.ToString() })); }