I have this function that basically uploads a video to YouTube. However, currently I am having to hard code the actual file path where the video is located(example: @"C:\Users\Peter\Desktop\audio\test.mp4";). I am wondering is there a way to make this more dynamic. For example right now I am using HTTP trigger, but how can the code be refactored to make it a Blob trigger? So that, when I upload a new .mp4 file into my blob storage container, this function would get triggered.
I am asking this because, I planning to move this function to Azure portal and there, I won't be able to specify local file path there like I am doing right now. Thanks in advance.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3.Data;
using System.Reflection;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using System.Threading;
namespace UploadVideoBlob
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([BlobTrigger("video/{name}")]Stream myBlob, string name, Microsoft.Azure.WebJobs.ExecutionContext context, ILogger log)
{
UserCredential credential;
using(var stream = new FileStream(System.IO.Path.Combine(context.FunctionDirectory, "client_secrets.json"), FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted";
var VideoInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", myBlob, "video/*");
await VideoInsertRequest.UploadAsync();
}
}
}
function.json
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.29",
"configurationSource": "attributes",
"bindings": [
{
"type": "blobTrigger",
"path": "video/{name}",
"name": "myBlob"
}
],
"disabled": false,
"scriptFile": "../bin/UploadVideoBlob.dll",
"entryPoint": "UploadVideoBlob.Function1.Run"
}
client_secrets.json
{
"installed": {
"client_id": "147300761218-dl0rhktkoj8arh0ebu5pu56es06hje5p.apps.googleusercontent.com",
"project_id": "mytestproj",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "xxxxxxxxxxxxxxxxxx",
"redirect_uris": [ "urn:ietf:wg:oauth:2.0:oob"]
}
}
