简体   繁体   English

Stream 视频来自 ZD7EFA19FBE7D3972FD74ADB6024Z223D 中的 Azure Function

[英]Stream video from Azure Function in C#

I'm trying to write an Azure HttpTrigger Function that will stream a video from blob storage.我正在尝试编写 Azure HttpTrigger Function ,它将 stream 来自 blob 存储的视频。

At the moment, I have the following code, but this does not stream.目前,我有以下代码,但这不是 stream。

    [FunctionName(nameof(GetVideo))]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "Video/{videoID}")] HttpRequestMessage request,
        ExecutionContext executionContext,
        ILogger log,
        string videoID)
    {
        IActionResult result;

        string storageConnectionString = _configurationSettings.GetStringValue("StorageConnectionString");

        if (!await _blobStorage.BlobExists(storageConnectionString, "Videos", videoID).ConfigureAwait(false))
        {
            result = new NotFoundResult();
        }
        else
        {
            string videoMimeType = _configurationSettings.GetStringValue("VideoMimeType", DefaultVideoMimeType);

            Stream stream = await _blobStorage.GetBlobAsStream(storageConnectionString, "Videos", videoID).ConfigureAwait(false);
            byte[] videoBytes = GetBytesFromStream(stream);

            result = new FileContentResult(videoBytes, videoMimeType);
        }

        return result;
    }

Can anyone help?任何人都可以帮忙吗?

What do you mean this does not stream , I tried to bind the input blob and return it, it succeeds.this does not stream是什么意思,我尝试绑定输入 blob 并返回它,它成功了。 Maybe you could refer to my below code.也许你可以参考我下面的代码。

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Blob("test/test.mp4", FileAccess.Read)] Stream myBlob,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");


            byte[] bytes = new byte[myBlob.Length];

            myBlob.Read(bytes, 0, bytes.Length);
            myBlob.Seek(0, SeekOrigin.Begin);

            var result = new FileContentResult(bytes, "video/mp4");
            return result;

        }

Update : Suppose you want to request the dynamic blob name in the HTTP request, you could refer to the below code, bind the container the get the blob and download it to bytes.更新:假设您想在 HTTP 请求中请求动态 blob 名称,您可以参考以下代码,将容器绑定到获取 blob 并将其下载到字节。 I pass the blob name in the HTTP request query parameter.我在 HTTP 请求查询参数中传递了 blob 名称。

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Blob("test", FileAccess.Read,Connection = "AzureWebJobsStorage")] CloudBlobContainer myBlob,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string filename = req.Query["filename"];
            CloudBlockBlob blob= myBlob.GetBlockBlobReference(filename);

            await blob.FetchAttributesAsync();

            long fileByteLength = blob.Properties.Length;
            byte[] fileContent = new byte[fileByteLength];
            for (int i = 0; i < fileByteLength; i++)
            {
                fileContent[i] = 0x20;
            }
            await blob.DownloadToByteArrayAsync(fileContent, 0);

            var result = new FileContentResult(fileContent, "video/mp4");
            return result;

        }

在此处输入图像描述

I did like below for minimum memory footprint (without keeping the full blob into bytes in memory).我确实喜欢下面的最小 memory 占用空间(没有将完整的 blob 保存到内存中的字节中)。 Note that instead of binding to stream, I am binding to a ICloudBlob instance (luckily, C# function supports several flavors of blob input binding ) and returning open stream. Note that instead of binding to stream, I am binding to a ICloudBlob instance (luckily, C# function supports several flavors of blob input binding ) and returning open stream. Tested it using memory profiler and works fine with no memory leak even for large blobs.使用 memory 分析器对其进行了测试,即使对于大斑点也没有 memory 泄漏。

NOTE: You don't need to seek to stream position 0 or flush or dispose (disposing would be automatically done on response end);注意:您不需要寻找 stream position 0 或刷新或处置(处置将在响应结束时自动完成);

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 Microsoft.Azure.Storage.Blob;

namespace TestFunction1
{
   public static class MyFunction
   {
        [FunctionName("MyFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "video/{fileName}")] HttpRequest req,
            [Blob("test/{fileName}", FileAccess.Read, Connection = "BlobConnection")] ICloudBlob blob,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var blobStream = await blob.OpenReadAsync().ConfigureAwait(false);
            return new FileStreamResult(blobStream, "video/mp4");
        }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM