【发布时间】:2020-06-27 23:07:34
【问题描述】:
我正在创建一个 Azure Blob 触发函数,其中包含每当“视频”容器中有视频文件时将视频上传到 YouTube 频道的代码。我使用的代码来自 YouTube API 示例代码:https://developers.google.com/youtube/v3/docs/videos/insert。我已获取此代码,并将其放入 Azure 函数中(代码如下)。当我在本地编译并运行该函数时,它只是说“作业已开始”(下面的控制台输出)。接下来,我尝试将此功能发布到 Azure 门户,并在我的“视频”容器中上传了一个 test.mp4 视频。当我在 Azure 门户中单击我的函数的“运行”按钮时,它会引发这样的错误,并且我的视频没有上传到我的 YouTube 频道。 ]1
这是我名为 uploadvideotoyoutube 的存储帐户的快照,它有一个“视频”容器。在“视频”容器内,我上传了一个 test.mp4 文件。 这是我的代码和其他文件:
函数1.cs:
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 YoutubeUploadFunction
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([BlobTrigger("video/{name}", Connection = "AzureWebJobsStorage")]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();
}
}
}
Local.Setting.Json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=uploadvideotoyoutube;AccountKey=xxxxxxxxxxxxxxxxxxxxxx==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
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"]
}
}
当我在本地编译和运行函数时,这是我在控制台中看到的输出:
%%%%%%
%%%%%%
@ %%%%%% @
@@ %%%%%% @@
@@@ %%%%%%%%%%% @@@
@@ %%%%%%%%%% @@
@@ %%%% @@
@@ %%% @@
@@ %% @@
%%
%
Azure Functions Core Tools (3.0.2245 Commit hash: 1d094e2f3ef79b9a478a1621ea7ec3f93ac1910d)
Function Runtime Version: 3.0.13139.0
[3/16/2020 6:02:11 PM] Building host: startup suppressed: 'False', configuration suppressed: 'False', startup operation id: 'a37bba12-9125-4af6-8c10-26daef57ef90'
[3/16/2020 6:02:11 PM] Reading host configuration file 'C:\Users\Sean\Desktop\UploadVideo\YoutubeUploadFunction\YoutubeUploadFunction\bin\Debug\netcoreapp3.0\host.json'
[3/16/2020 6:02:11 PM] Host configuration file read:
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "version": "2.0"
[3/16/2020 6:02:11 PM] }
[3/16/2020 6:02:11 PM] Reading functions metadata
[3/16/2020 6:02:11 PM] 1 functions found
[3/16/2020 6:02:11 PM] Loading startup extension 'AzureStorage'
[3/16/2020 6:02:11 PM] Loaded extension 'AzureStorage' (3.0.4.0)
[3/16/2020 6:02:11 PM] Initializing Warmup Extension.
[3/16/2020 6:02:11 PM] Initializing Host. OperationId: 'a37bba12-9125-4af6-8c10-26daef57ef90'.
[3/16/2020 6:02:11 PM] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=a37bba12-9125-4af6-8c10-26daef57ef90
[3/16/2020 6:02:11 PM] LoggerFilterOptions
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "MinLevel": "None",
[3/16/2020 6:02:11 PM] "Rules": [
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "ProviderName": null,
[3/16/2020 6:02:11 PM] "CategoryName": null,
[3/16/2020 6:02:11 PM] "LogLevel": null,
[3/16/2020 6:02:11 PM] "Filter": "<AddFilter>b__0"
[3/16/2020 6:02:11 PM] },
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[3/16/2020 6:02:11 PM] "CategoryName": null,
[3/16/2020 6:02:11 PM] "LogLevel": "None",
[3/16/2020 6:02:11 PM] "Filter": null
[3/16/2020 6:02:11 PM] },
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[3/16/2020 6:02:11 PM] "CategoryName": null,
[3/16/2020 6:02:11 PM] "LogLevel": null,
[3/16/2020 6:02:11 PM] "Filter": "<AddFilter>b__0"
[3/16/2020 6:02:11 PM] }
[3/16/2020 6:02:11 PM] ]
[3/16/2020 6:02:11 PM] }
[3/16/2020 6:02:11 PM] FunctionResultAggregatorOptions
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "BatchSize": 1000,
[3/16/2020 6:02:11 PM] "FlushTimeout": "00:00:30",
[3/16/2020 6:02:11 PM] "IsEnabled": true
[3/16/2020 6:02:11 PM] }
[3/16/2020 6:02:11 PM] SingletonOptions
[3/16/2020 6:02:11 PM] {
[3/16/2020 6:02:11 PM] "LockPeriod": "00:00:15",
[3/16/2020 6:02:11 PM] "ListenerLockPeriod": "00:00:15",
[3/16/2020 6:02:11 PM] "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
[3/16/2020 6:02:12 PM] "LockAcquisitionPollingInterval": "00:00:05",
[3/16/2020 6:02:12 PM] "ListenerLockRecoveryPollingInterval": "00:01:00"
[3/16/2020 6:02:12 PM] }
[3/16/2020 6:02:12 PM] QueuesOptions
[3/16/2020 6:02:12 PM] {
[3/16/2020 6:02:12 PM] "BatchSize": 16,
[3/16/2020 6:02:12 PM] "NewBatchThreshold": 8,
[3/16/2020 6:02:12 PM] "MaxPollingInterval": "00:00:02",
[3/16/2020 6:02:12 PM] "MaxDequeueCount": 5,
[3/16/2020 6:02:12 PM] "VisibilityTimeout": "00:00:00"
[3/16/2020 6:02:12 PM] }
[3/16/2020 6:02:12 PM] BlobsOptions
[3/16/2020 6:02:12 PM] {
[3/16/2020 6:02:12 PM] "CentralizedPoisonQueue": false
[3/16/2020 6:02:12 PM] }
[3/16/2020 6:02:12 PM] HttpOptions
[3/16/2020 6:02:12 PM] {
[3/16/2020 6:02:12 PM] "DynamicThrottlesEnabled": false,
[3/16/2020 6:02:12 PM] "MaxConcurrentRequests": -1,
[3/16/2020 6:02:12 PM] "MaxOutstandingRequests": -1,
[3/16/2020 6:02:12 PM] "RoutePrefix": "api"
[3/16/2020 6:02:12 PM] }
[3/16/2020 6:02:12 PM] Starting JobHost
[3/16/2020 6:02:12 PM] Starting Host (HostId=desktopgq271u4-950774370, InstanceId=16bc66b5-e751-4c00-b383-6f705e303c13, Version=3.0.13139.0, ProcessId=5656, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=(null))
[3/16/2020 6:02:12 PM] Loading functions metadata
[3/16/2020 6:02:12 PM] 1 functions loaded
[3/16/2020 6:02:12 PM] Generating 1 job function(s)
[3/16/2020 6:02:12 PM] Found the following functions:
[3/16/2020 6:02:12 PM] YoutubeUploadFunction.Function1.Run
[3/16/2020 6:02:12 PM]
[3/16/2020 6:02:14 PM] Initializing function HTTP routes
[3/16/2020 6:02:14 PM] No HTTP routes mapped
[3/16/2020 6:02:14 PM]
[3/16/2020 6:02:14 PM] Host initialized (2033ms)
[3/16/2020 6:02:14 PM] Host started (2485ms)
[3/16/2020 6:02:14 PM] Job host started
Hosting environment: Production
Content root path: C:\Users\Sean\Desktop\UploadVideo\YoutubeUploadFunction\YoutubeUploadFunction\bin\Debug\netcoreapp3.0
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
[3/16/2020 6:02:19 PM] Host lock lease acquired by instance ID '000000000000000000000000A74A8599'.
C:\Users\Sean\AppData\Local\AzureFunctionsTools\Releases\3.4.1\cli_x64\func.exe (process 5656) exited with code -1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
如何让该功能运行,以便将视频上传到我的 YouTube 频道?我的功能需要改变吗?谢谢你。
【问题讨论】:
-
你在本地运行函数的时候,有没有看到你的函数被blob触发了?
-
@GeorgeChen 我使用了 Azure 存储资源管理器,并将示例视频上传到我的容器,它似乎触发了,因为我看到了这个:2020-03-17T01:48:16.534 [信息] Executing 'Function1 ' (Reason='New blob detected: video/test.mp4', Id=cf36ef6e-1274-4be4-a5ba-86acc1dc937c) 但是,为什么没有将 test.mp4 上传到我的 Youtube 频道?
-
@GeorgeChen 能否请您看一下我的 Azure 函数代码,看看是否缺少某些内容或是否可以改进?
-
假设你的代码没有问题,即使有问题,也应该触发异常。所以尝试重新启动你的函数,然后将文件上传到 blob 容器,检查你的函数是否可以被触发。
-
而且你不需要手动运行它,如果点击运行我会得到和你一样的结果,因为没有处理任何blob。
标签: .net azure google-api youtube-api azure-functions