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!"); 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() })); }