使用Azure Blob存储中的.Net Core API异步流式传输视频 [英] Asynchronously Streaming Video with .Net Core API from Azure Blob Storage

查看:135
本文介绍了使用Azure Blob存储中的.Net Core API异步流式传输视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多示例,这些示例使用了我的应用程序中不可用的对象,并且似乎与我的.NET Core Web API版本不匹配.本质上,我正在一个项目上,该项目将在网页上具有标签,并且想要使用来自服务器的流而不是通过路径直接提供文件来加载视频.原因之一是文件的来源可能会更改,并且通过路径提供文件不是我的客户想要的.因此,我需要能够打开流并异步写入视频文件.

I have found a bunch of examples that use objects not available to me within my application and don't seem to match up to my version of .NET Core web API. In essence, I am working on a project that will have tags on a web page and want to load the videos using a stream from the server rather than directly serving the files via a path. One reason is the source of the files may change and serving them via path isn't what my customer wants. So I need to be able to open a stream and async write the video file.

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

This for some reason produces JSON data so that's wrong. I am downloading the video file from Azure Blob storage and returning as a stream, but I just don't understand what I need to do to send a streamed video file to a tag in HTML.

我的API控制器,

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

我的服务班级,

 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控制器:

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

服务类别:

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>

这篇关于使用Azure Blob存储中的.Net Core API异步流式传输视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆