Created
June 16, 2023 12:56
-
-
Save ajtatum/a385443cfea376fba1b5fb041331787d to your computer and use it in GitHub Desktop.
Upload to Mux API via Azure Function with Blob Trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Extensions.Logging; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using System.Text.Json.Serialization; | |
namespace AJTDigital.Mux | |
{ | |
public class DirectMuxVideoBlobTrigger | |
{ | |
private const string BaseMuxApiUrl = "https://api.mux.com/video/v1/assets"; | |
private const string BaseVideosContainerUrl = "https://myazurestorage.blob.core.windows.net/videos"; | |
[FunctionName("DirectMuxVideoBlobTrigger")] | |
public async Task Run([BlobTrigger("videos/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log) | |
{ | |
var muxTokenId = Environment.GetEnvironmentVariable("MUX_TOKEN_ID"); | |
var muxTokenSecret = Environment.GetEnvironmentVariable("MUX_TOKEN_SECRET"); | |
var authenticationString = $"{muxTokenId}:{muxTokenSecret}"; | |
var base64String = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(authenticationString)); | |
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, BaseMuxApiUrl); | |
httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64String); | |
var muxVideo = new MuxAsset(); | |
muxVideo.Input = $"{BaseVideosContainerUrl}/{name}"; | |
using(var httpClient = new HttpClient()) | |
{ | |
var request = await httpClient.SendAsync(httpRequestMessage); | |
request.EnsureSuccessStatusCode(); | |
} | |
log.LogInformation($"Mux Video {name} sent to Mux API."); | |
} | |
} | |
public class MuxAsset | |
{ | |
[JsonPropertyName("cors_origin")] | |
public string CorsOrigin { get; set; } = "*"; | |
[JsonPropertyName("input")] | |
public string Input { get; set; } | |
[JsonPropertyName("mp4_support")] | |
public string MP4Support { get; set; } = "standard"; | |
[JsonPropertyName("playback_policy")] | |
public string PlaybackPolicy { get; set; } = "public"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Extensions.Logging; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace AJTDigital.Mux | |
{ | |
public class MuxVideoBlobTrigger | |
{ | |
private const string VideosContainerUrl = "https://myazurestorage.blob.core.windows.net/videos"; | |
[FunctionName("MuxVideoBlobTrigger")] | |
public async Task Run([BlobTrigger("videos/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log) | |
{ | |
string storageVideoUrl = $"{VideosContainerUrl}/{name}"; | |
var muxVideo = new MuxVideo(); | |
muxVideo.VideoUrl = storageVideoUrl; | |
using(var httpClient = new HttpClient()) | |
{ | |
var request = await httpClient.PostAsJsonAsync($"{Environment.GetEnvironmentVariable("AzureLogicAppTriggerUrl")}&fileUrl={storageVideoUrl}", muxVideo); | |
request.EnsureSuccessStatusCode(); | |
} | |
log.LogInformation($"Mux Video {name} sent to Azure Logic App for processing."); | |
} | |
} | |
public class MuxVideo | |
{ | |
public string VideoUrl { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment