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
            }));
        }