Example #1
0
        public static async Task Run(
            [BlobTrigger("ingest/{name}", Connection = "IngestStorage")] Stream blob,
            string name,
            ExecutionContext context,
            ILogger log)
        {
            log.LogInformation("Blob Trigger - Received Blob '{Name}', Size: {Length} bytes", name, blob.Length);
            var client = new MediaServiceClient(context.GetMediaServiceConfiguration(), log);
            await client.LoginAsync().ConfigureAwait(false);

            // Create unique asset names
            string uniqueness      = Guid.NewGuid().ToString().Substring(0, 10).Replace("-", string.Empty);
            string jobName         = "job-" + uniqueness;
            string inputAssetName  = "input-" + uniqueness;
            string outputAssetName = "output-" + uniqueness;

            // Create input asset and upload blob
            var inputAsset = await client.CreateAssetFromBlobAsync(inputAssetName, name, blob)
                             .ConfigureAwait(false);

            // Check if an output Asset already exists. If it does, define a new unique name
            Asset outputAsset = await client.GetAssetAsync(outputAssetName)
                                .ConfigureAwait(false);

            if (outputAsset != null)
            {
                outputAssetName = $"{name}-{Guid.NewGuid()}".ToLower();
            }

            // Create output asset
            await client.CreateOrUpdateAssetAsync(outputAssetName, new Asset()).ConfigureAwait(false);

            // Create encoding pipeline
            var transform = await client.GetOrCreateTransformAsync("Content Adaptive Multiple Bitrate MP4")
                            .ConfigureAwait(false);

            // Submit a new encoding job, transforming the input Asset and placing
            // the results in the output Asset
            await client.SubmitJobAsync(transform.Name, jobName, inputAssetName, outputAssetName)
            .ConfigureAwait(false);
        }
        public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            ExecutionContext context,
            ILogger log)
        {
            // Dump the event details
            log.LogInformation($"EventGridEvent" +
                               "\n\tId: {EventId}" +
                               "\n\tTopic: {EventTopic}" +
                               "\n\tSubject: {EventSubject}" +
                               "\n\tType: {EventType}" +
                               "\n\tData: {EventData}",
                               eventGridEvent.Id,
                               eventGridEvent.Topic,
                               eventGridEvent.Subject,
                               eventGridEvent.EventType,
                               eventGridEvent.Data.ToString());

            var client = new MediaServiceClient(context.GetMediaServiceConfiguration(), log);
            await client.LoginAsync().ConfigureAwait(false);

            // Find the right event
            if (eventGridEvent.EventType == "Microsoft.Media.JobOutputStateChange")
            {
                log.LogInformation("Detected JobOutputStateChange event");

                dynamic data   = eventGridEvent.Data;
                var     output = data.output;

                // Only react if this is the last step in the process
                if (output.state == "Finished")
                {
                    string assetName = output.assetName;
                    log.LogInformation("Publishing asset '{AssetName}'", assetName);
                    await client.PublishAsset(assetName, "Predefined_ClearStreamingOnly")
                    .ConfigureAwait(false);
                }
            }
        }