Azure函数由于文件路径而无法在本地运行?

Azure函数由于文件路径而无法在本地运行?,azure,.net-core,youtube-api,azure-functions,azure-logic-apps,Azure,.net Core,Youtube Api,Azure Functions,Azure Logic Apps,当我将新视频上传到Azure Blob容器中时,我希望该视频也能使用Azure Logic应用程序自动上传到我的YouTube频道 为此,首先我创建了这个Azure函数,其中包含自动将视频上传到我的YouTube频道()的代码。我将此设置为HTTP触发器函数(不确定是否使用了正确的触发器),当在我的Azure Blob容器中上载新视频时,应触发此函数 当我运行应用程序时,我收到一个错误: 执行“功能1”(失败,Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f) Sy

当我将新视频上传到Azure Blob容器中时,我希望该视频也能使用Azure Logic应用程序自动上传到我的YouTube频道

为此,首先我创建了这个Azure函数,其中包含自动将视频上传到我的YouTube频道()的代码。我将此设置为HTTP触发器函数(不确定是否使用了正确的触发器),当在我的Azure Blob容器中上载新视频时,应触发此函数

当我运行应用程序时,我收到一个错误:

执行“功能1”(失败,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\u secrets.json'

问题:这个错误是什么意思?我如何重构代码,使之成为一个可访问的文件路径,以便每当Azure blob容器中有新视频时,就会触发此函数并将视频上载到我的YouTube频道

代码:

使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Google.api.Auth.OAuth2;
使用Google.api.Upload;
使用Google.api.YouTube.v3.Data;
运用系统反思;
使用Google.api.YouTube.v3;
使用Google.api.Services;
使用系统线程;
命名空间上载视频
{
公共静态类函数1
{
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
登录信息(“YouTube数据API:上传视频”);
日志。登录信息(“====================================================”);
尝试
{
等待运行();
}
捕获(聚合异常)
{
foreach(ex.InnerExceptions中的变量e)
{
登录信息(“错误:+e.Message”);
}
}
返回新的OkObjectResult($“视频处理…”);
}
专用静态异步任务运行()
{
用户凭证;
使用(var stream=newfilestream(“client_secrets.json”、FileMode.Open、FileAccess.Read))
{
凭证=等待GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
//此OAuth 2.0访问范围允许应用程序将文件上载到
//已验证用户的YouTube频道,但不允许其他类型的访问。
新[]{YouTubeService.Scope.YoutubeUpload},
“用户”,
取消令牌。无
);
}
var youtubeService=new youtubeService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=Assembly.GetExecutionGassembly().GetName().Name
});
var video=新视频();
video.Snippet=新的VideoSnippet();
video.Snippet.Title=“默认视频标题”;
video.Snippet.Description=“默认视频描述”;
video.Snippet.Tags=新字符串[]{“tag1”、“tag2”};
video.Snippet.CategoryId=“22”//请参阅https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status=新的VideoStatus();
video.Status.PrivacyStatus=“未列出”;//或“私有”或“公共”
var filePath=@“C:\Users\Peter\Desktop\audio\test.mp4”//替换为实际电影文件的路径。
使用(var fileStream=newfilestream(filePath,FileMode.Open))
{
var videosInsertRequest=youtubeService.Videos.Insert(视频,“片段,状态”,文件流,“视频/*”;
videosInsertRequest.ProgressChanged+=videosInsertRequest\u ProgressChanged;
videosInsertRequest.ResponseReceived+=videosInsertRequest_ResponseReceived;
等待videosInsertRequest.UploadAsync();
}
}
私有静态void videosInsertRequest_ProgressChanged(Google.api.Upload.IUploadProgress)
{
开关(进度状态)
{
案例上传状态。上传:
WriteLine(“{0}字节已发送。”,progress.BytesSent);
打破
案例上载状态。失败:
WriteLine(“一个错误阻止上载完成。\n{0}”,progress.Exception);
打破
}
}
私有静态无效视频接收请求_响应(视频)
{
WriteLine(“视频id{0}已成功上载。”,Video.id);
}
}
}

它能在Azure上运行,但不能在本地机器上运行吗?还是?@Frank Nielsen我先在本地机器上试用了它,但它本身就抛出了一个错误。我不确定我的GET请求是否错误,或者我的代码是否错误。这是我正在输入的GET请求:是的,但是您正在执行一个
文件流(“client_secrets.json”
,因此它会加载与执行路径相关的文件。您在异常中显示的路径是在您的本地计算机上,我猜是的!@FrankNielsen,是的,我检查过了,路径是在我的本地计算机上的?我如何修复此问题?如果上述语句在Azure中起作用(?),并且您只有在本地运行时遇到问题,您可能会使用编译指令
#如果DEBUG
和硬编码另一个pat
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);
        }
    }
}