繁体   English   中英

使用 .Net Core API 从 Azure Blob 存储异步流式传输视频

[英]Asynchronously Streaming Video with .Net Core API from Azure Blob Storage

我发现了一堆示例,这些示例在我的应用程序中使用了我不可用的对象,并且似乎与我的 .NET Core web API 版本不匹配。本质上,我正在开发一个项目,该项目将在 web 上添加标签页面并希望使用 stream 从服务器加载视频,而不是直接通过路径提供文件。 原因之一是文件的来源可能会改变,通过路径提供服务并不是我的客户想要的。 所以我需要能够打开 stream 并异步写入视频文件。

由于某种原因,这会产生 JSON 数据,所以这是错误的。 我正在从 Azure Blob 存储下载视频文件并作为 stream 返回,但我只是不明白我需要做什么才能将流式视频文件发送到 HTML 中的标签。

我的 API Controller,

[AllowAnonymous]
    [HttpGet("getintroductoryvideos")]
    public async Task<Stream> GetIntroductoryVideos()
    {
        try
        {
            return  _documentsService.WriteContentToStream().Result;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我的服务 class,

 public async Task<Stream> WriteContentToStream()
    {
        var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
        await cloudBlob.FetchAttributesAsync();

        var fileStream = new MemoryStream();
        await cloudBlob.DownloadToStreamAsync(fileStream);
        return fileStream;
    }

你可以试试下面的代码:

API Controller:

[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<FileContentResult> GetIntroductoryVideos(string videoname)
{        
   return  await _documentsService.WriteContentToStream();        
}

服务 class:

public async Task<FileContentResult> WriteContentToStream()
{
    var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);

    MemoryStream fileStream = new MemoryStream();
    await cloudBlob.DownloadToStreamAsync(fileStream);
    return new FileContentResult (fileStream.ToArray(), "application/octet-stream");

}

Html:

<div className="xxx">
  <video height="auto">
      <source src="xx/getintroductoryvideos?videoname=xxx" type="video/mp4" />
  </video>
</div>

您可能希望在返回之前避免在 memory 中加载整个视频。 您应该能够通过 stream 使用FileStreamResult

[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<IActionResult> GetIntroductoryVideos()
{
  var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
  var stream = await cloudBlob.OpenReadAsync();
  return new FileStreamResult(stream, "application/octet-stream");
}

这是我所做的。 重点是最后一行。 那里的最后一个参数启用名为EnableRangeProcessing的范围请求。 虽然这从 .net 核心 2.1 和 2.1 plus 开始支持。

[AllowAnonymous]
[HttpGet("getintroductoryvideos")]
public async Task<IActionResult> GetIntroductoryVideos()
{
  var cloudBlob = await _blobService.GetBlobAsync(PlatformServiceConstants._blobIntroductoryVideoContainerPath + PlatformServiceConstants.IntroductoryVideo1, introductoryvideocontainerName);
  var stream = await cloudBlob.OpenReadAsync();
  return new File(stream, "application/octet-stream",true);
}

暂无
暂无

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

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