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

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

问题描述

我基本上是在创建HTTP Trigger 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:\Users\Peter\Desktop\TestDemo\UploadVideo\UploadVideo\bin\Debug\netcoreapp2.1 \client_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:\Users\Peter\Desktop\TestDemo\UploadVideo\UploadVideo\bin\Debug\netcoreapp2.1\client_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:\Users\Peter\Desktop\audio\test.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.\n{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.

然后从您的错误中进行操作:

And from your error:


执行了 Function1(失败,Id = 84400f0c-b6e4 -4c78-bf55-30c4527a8b5f)
System.Private.CoreLib:执行功能:Function1时发生异常。
System.Private.CoreLib:找不到文件
'C:\Users\Peter\Desktop\TestDemo\UploadVideo\UploadVideo\bin\Debug\netcoreapp2.1 \client_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:\Users\Peter\Desktop\TestDemo\UploadVideo\UploadVideo\bin\Debug\netcoreapp2.1\client_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天全站免登陆