无法成功运行包含将视频上传到 YouTube 的代码的 Azure 函数 [英] Not able to successfully run an Azure Function which contains code to Upload Video to YouTube

查看:20
本文介绍了无法成功运行包含将视频上传到 YouTube 的代码的 Azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是在创建一个 HTTP 触发器 Azure 函数,该函数具有使用 YouTube API 将视频上传到 YouTube 的代码.该代码几乎由 YouTubeAPI 文档提供:https://developers.google.com/youtube/v3/docs/videos/insert.我重新格式化了代码以使其适合 Azure 函数.

I am basically creating an HTTP Trigger Azure function, which has the code to upload a video to YouTube using the YouTube API. The code was pretty much provided by the YouTubeAPI docs: https://developers.google.com/youtube/v3/docs/videos/insert. I re-formatted the code a bit to fit it into the Azure function.

但是,当我尝试在 Visual Studio 本地运行该函数时,我收到 500 错误消息:

However, when I try to run the function locally on my Visual Studio, I get an 500 error saying:

执行'Function1'(失败,Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f)System.Private.CoreLib:执行函数时出现异常:Function1.System.Private.CoreLib:找不到文件'C:UsersPeterDesktopTestDemoUploadVideoUploadVideoinDebug etcoreapp2.1client_secrets.json'.

Executed 'Function1' (Failed, Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f) System.Private.CoreLib: Exception while executing function: Function1. System.Private.CoreLib: Could not find file 'C:UsersPeterDesktopTestDemoUploadVideoUploadVideoinDebug etcoreapp2.1client_secrets.json'.

我不确定如何修复此错误并使函数运行而没有任何错误.是否需要在代码(如下)中添加/更改任何内容来解决此问题?

I am not sure how to fix this error and to make the function run without any errors. Is there anything that needs to be added/changed in the code (below) to fix this issue?

我的目标:我的最终目标是在 Azure Blob 存储中添加新视频时触发此 azure 函数.

My goal: my ultimate goal is to trigger this azure function whenever there is a new video added into the Azure Blob Storage.

代码

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 UploadVideo
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            log.LogInformation("YouTube Data API: Upload Video");
            log.LogInformation("==============================");

            try
            {
                await Run();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    log.LogInformation("Error: " + e.Message);
                }
            }

            return new OkObjectResult($"Video Processed..");

        }

        private static async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    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"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
            var filePath = @"C:UsersPeterDesktopaudio	est.mp4"; // Replace with path to actual movie file.

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

                await videosInsertRequest.UploadAsync();
            }
        }

        private static void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
        {
            switch (progress.Status)
            {
                case UploadStatus.Uploading:
                    Console.WriteLine("{0} bytes sent.", progress.BytesSent);
                    break;

                case UploadStatus.Failed:
                    Console.WriteLine("An error prevented the upload from completing.
{0}", progress.Exception);
                    break;
            }
        }

        private static void videosInsertRequest_ResponseReceived(Video video)
        {
            Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
        }
    }
}

推荐答案

首先,从您的需求出发:

First, from your requirements:

我的目标:我的最终目标是在任何时候触发这个 azure 函数Azure Blob 存储中添加了一个新视频.

My goal: my ultimate goal is to trigger this azure function whenever there is a new video added into the Azure Blob Storage.

您需要使用 Azure blob 触发器来实现您的目标.

You need to use Azure blob trigger to achieve your goal.

从你的错误来看:

执行'Function1'(失败,Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f)System.Private.CoreLib:执行函数时出现异常:Function1.System.Private.CoreLib:找不到文件'C:UsersPeterDesktopTestDemoUploadVideoUploadVideoinDebug etcoreapp2.1client_secrets.json'.

Executed 'Function1' (Failed, Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f) System.Private.CoreLib: Exception while executing function: Function1. System.Private.CoreLib: Could not find file 'C:UsersPeterDesktopTestDemoUploadVideoUploadVideoinDebug etcoreapp2.1client_secrets.json'.

我认为您在创建 json 文件后没有将其属性更改为如果更新则复制".如果您不更改此属性,您将面临此错误.所以它与您的代码在同一文件夹中,但与您的 dll 文件不在同一文件夹中.

I think you didn't change the properties of the json file to 'Copy if newer' after you create it. If you don't change this property, you will face this error. So it is in the same folder with your code but not in the same folder with your dll file.

这篇关于无法成功运行包含将视频上传到 YouTube 的代码的 Azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆