public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); if (data.containerName == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass the \"containerName\" property in the input object" })); } try { CloudBlobContainer container = BlobStorageHelper.GetCloudBlobContainer(_storageAccountName, _storageAccountKey, data.containerName.ToString()); container.Delete(); } catch (System.Exception ex) { log.Error("Error when trying to delete the container", ex); throw; } return(req.CreateResponse(HttpStatusCode.OK)); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info($"AMS v3 Function - move_blob was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); if (data.inputContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass the input container name in the input object" })); } if (data.outputContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass the output container name in the input object" })); } try { CloudBlobContainer destination = BlobStorageHelper.GetCloudBlobContainer(_storageAccountName, _storageAccountKey, data.inputContainer.ToString()); CloudBlobContainer source = BlobStorageHelper.GetCloudBlobContainer(_storageAccountName, _storageAccountKey, data.outputContainer.ToString()); IEnumerable <IListBlobItem> blobs = source.ListBlobs("", useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Copy); var fileNames = new List <string>(); foreach (CloudBlockBlob blob in blobs) { fileNames.Add(blob.Name); } BlobStorageHelper.CopyBlobsAsync(source, destination, fileNames, log); source.Delete(); } catch (System.Exception ex) { log.Error("Error when trying to move blobs", ex); throw; } return(req.CreateResponse(HttpStatusCode.OK)); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"AMS v3 Function - start_blob_copy_to_asset was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); // Validate input objects if (data.assetName == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetName in the input object" })); } if (data.assetId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId 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 - assetName : " + data.assetName); log.Info("Input - assetId : " + data.assetId); log.Info("Input - SourceStorageAccountName : " + data.sourceStorageAccountName); log.Info("Input - SourceStorageAccountKey : " + data.sourceStorageAccountKey); string assetName = data.assetName; string assetId = data.assetId; string _sourceStorageAccountName = data.sourceStorageAccountName; string _sourceStorageAccountKey = data.sourceStorageAccountKey; string sourceContainerName = data.sourceContainer; List <string> fileNames = null; if (data.fileNames != null) { fileNames = ((JArray)data.fileNames).ToObject <List <string> >(); } MediaServicesConfigWrapper amsconfig = new MediaServicesConfigWrapper(); Asset asset = null; string destinationContainer = ""; try { IAzureMediaServicesClient client = CreateMediaServicesClient(amsconfig); asset = client.Assets.Get(amsconfig.ResourceGroup, amsconfig.AccountName, assetName); if (asset == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } // Setup blob container CloudBlobContainer sourceBlobContainer = BlobStorageHelper.GetCloudBlobContainer(_sourceStorageAccountName, _sourceStorageAccountKey, sourceContainerName); sourceBlobContainer.CreateIfNotExists(); var response = client.Assets.ListContainerSas(amsconfig.ResourceGroup, amsconfig.AccountName, assetName, permissions: AssetContainerPermission.ReadWrite, expiryTime: DateTime.UtcNow.AddHours(4).ToUniversalTime()); var sasUri = new Uri(response.AssetContainerSasUrls.First()); CloudBlobContainer destinationBlobContainer = new CloudBlobContainer(sasUri); destinationContainer = destinationBlobContainer.Name; // Copy Source Blob container into Destination Blob container that is associated with the asset. BlobStorageHelper.CopyBlobsAsync(sourceBlobContainer, destinationBlobContainer, fileNames, log); } catch (ApiErrorException e) { log.Info($"ERROR: AMS API call failed with error code: {e.Body.Error.Code} and message: {e.Body.Error.Message}"); return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "AMS API call error: " + e.Message })); } return(req.CreateResponse(HttpStatusCode.OK, new { destinationContainer = destinationContainer })); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"AMS v3 Function - monitor_blob_copy_container_status was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); // Validate input objects if (data.destinationContainer == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass destinationContainer in the input object" })); } string destinationContainerName = data.destinationContainer; List <string> fileNames = null; if (data.fileNames != null) { fileNames = ((JArray)data.fileNames).ToObject <List <string> >(); } MediaServicesConfigWrapper amsconfig = new MediaServicesConfigWrapper(); bool copyStatus = true; //CopyStatus copyStatus = CopyStatus.Success; try { CloudBlobContainer destinationBlobContainer = BlobStorageHelper.GetCloudBlobContainer(_storageAccountName, _storageAccountKey, destinationContainerName); 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; copyStatus = false; } 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; copyStatus = false; } // else we completed this pending copy } } catch (Exception e) { log.Info($"ERROR: Exception {e}"); return(req.CreateResponse(HttpStatusCode.BadRequest)); } return(req.CreateResponse(HttpStatusCode.OK, new { CopyStatus = copyStatus })); }