使用 .Net Core API 从 Azure Blob 存储异步流式传输视频
Asynchronously Streaming Video with .Net Core API from Azure Blob Storage
我发现了一堆示例,这些示例使用了我的应用程序中不可用的对象,并且似乎与我的 .NET Core web 版本不匹配 API。本质上,我正在做一个项目,该项目将在网页上有标签,并希望使用来自服务器的流加载视频,而不是直接通过路径提供文件。原因之一是文件的来源可能会改变,通过路径提供服务并不是我的客户想要的。所以我需要能够打开流并异步写入视频文件。
出于某种原因,这会产生 JSON 数据,所以这是错误的。我正在从 Azure Blob 存储下载视频文件并作为流返回,但我只是不明白我需要做什么才能将流式视频文件发送到 HTML.
中的标记
我的API控制器,
[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 控制器:
[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>
您可能希望避免在返回之前将整个视频加载到内存中。您应该能够使用 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 core 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);
}
我发现了一堆示例,这些示例使用了我的应用程序中不可用的对象,并且似乎与我的 .NET Core web 版本不匹配 API。本质上,我正在做一个项目,该项目将在网页上有标签,并希望使用来自服务器的流加载视频,而不是直接通过路径提供文件。原因之一是文件的来源可能会改变,通过路径提供服务并不是我的客户想要的。所以我需要能够打开流并异步写入视频文件。
出于某种原因,这会产生 JSON 数据,所以这是错误的。我正在从 Azure Blob 存储下载视频文件并作为流返回,但我只是不明白我需要做什么才能将流式视频文件发送到 HTML.
中的标记我的API控制器,
[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 控制器:
[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>
您可能希望避免在返回之前将整个视频加载到内存中。您应该能够使用 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 core 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);
}