C# 在C中从Azure函数流式传输视频#

C# 在C中从Azure函数流式传输视频#,c#,.net-core,video-streaming,azure-functions,C#,.net Core,Video Streaming,Azure Functions,我正在尝试编写一个Azure HttpTrigger函数,它将从blob存储流式传输视频 目前,我有以下代码,但这不是流 [FunctionName(nameof(GetVideo))] public async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "Video/{videoID}")] HttpRequestMessage

我正在尝试编写一个Azure HttpTrigger函数,它将从blob存储流式传输视频

目前,我有以下代码,但这不是流

    [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;
    }
[FunctionName(nameof(GetVideo))]
公共异步任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“GET”,Route=“Video/{videoID}”)]HttpRequestMessage请求,
ExecutionContext ExecutionContext,
伊洛格日志,
字符串(视频ID)
{
结果;
字符串storageConnectionString=_configurationSettings.GetStringValue(“storageConnectionString”);
如果(!await _blobStorage.BlobExists(存储连接字符串,“视频”,视频ID).ConfigureAwait(false))
{
结果=新的NotFoundResult();
}
其他的
{
字符串videoMimeType=\u configurationSettings.GetStringValue(“videoMimeType”,DefaultVideoMimeType);
Stream Stream=await\u blobStorage.GetBlobAsStream(storageConnectionString,“Videos”,videoID)。ConfigureAwait(false);
字节[]视频字节=GetBytesFromStream(流);
结果=新的FileContentResult(videoBytes,videoMimeType);
}
返回结果;
}

有人能帮忙吗?

你是什么意思
这不是流
,我尝试绑定输入blob并返回它,成功了。也许你可以参考我下面的代码

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;

        }
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[Blob(“test/test.mp4”,FileAccess.Read)]Stream myBlob,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
byte[]bytes=新字节[myBlob.Length];
myBlob.Read(字节,0,字节.长度);
myBlob.Seek(0,SeekOrigin.Begin);
var result=新的FileContentResult(字节,“视频/mp4”);
返回结果;
}
更新:假设您希望在HTTP请求中请求动态blob名称,您可以参考下面的代码,绑定容器以获取blob并将其下载到字节。我在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;

        }
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[Blob(“test”,FileAccess.Read,Connection=“azurewebjobstorage”)]CloudBlobContainer myBlob,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串文件名=请求查询[“文件名”];
CloudBlockBlob blob=myBlob.GetBlockBlobReference(文件名);
等待blob.FetchAttributesAsync();
long filebytellength=blob.Properties.Length;
byte[]fileContent=新字节[filebytellength];
for(int i=0;i

你是什么意思
这不是流
,我尝试绑定输入blob并返回它,它成功了。也许你可以参考我下面的代码

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;

        }
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[Blob(“test/test.mp4”,FileAccess.Read)]Stream myBlob,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
byte[]bytes=新字节[myBlob.Length];
myBlob.Read(字节,0,字节.长度);
myBlob.Seek(0,SeekOrigin.Begin);
var result=新的FileContentResult(字节,“视频/mp4”);
返回结果;
}
更新:假设您希望在HTTP请求中请求动态blob名称,您可以参考下面的代码,绑定容器以获取blob并将其下载到字节。我在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;

        }
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
[Blob(“test”,FileAccess.Read,Connection=“azurewebjobstorage”)]CloudBlobContainer myBlob,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串文件名=请求查询[“文件名”];
CloudBlockBlob blob=myBlob.GetBlockBlobReference(文件名);
等待blob.FetchAttributesAsync();
long filebytellength=blob.Properties.Length;
byte[]fileContent=新字节[filebytellength];
for(int i=0;i

我喜欢下面的方法,以获得最小的内存占用空间(不需要将整个blob保存到内存中的字节中)。请注意,我没有绑定到流,而是绑定到一个
ICloudBlob
实例(幸运的是,C#函数支持多种类型的)并返回开放流。使用内存分析器对其进行了测试,工作正常,即使对于大的blob也没有内存泄漏

注意:您不需要寻求流位置0或刷新或处置(处置将在响应端自动完成)

使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Microsoft.Azure.Storage.Blob;
命名空间TestFunction1
{
公共静态cla