c# - 从 Azure Blob 存储流式传输视频
问题描述
我已经尝试了很长时间,但似乎找不到在任何地方记录(或演示)的完美方法
到目前为止,这是我的代码;它从这里和这里以及其他地方借来了一些东西
[HttpGet("File/{id}")]
public IActionResult Index(int id)
{
var file = Files(a => a.AttachmentId == id);
if (file != null)
{
//using Azure.Storage.Blobs (v12)
BlobServiceClient blobServiceClient = new BlobServiceClient(config.GetConnectionString("azBlobConnection"));
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(file.BlobContainer);
BlobClient blobClient = containerClient.GetBlobClient(file.FileName);
MemoryStream memory = new MemoryStream();
blobClient.DownloadTo(memory, null, new StorageTransferOptions {
InitialTransferLength = 1024 * 1024,
MaximumConcurrency = 20,
MaximumTransferLength = 4 * 1024 * 1024
});
memory.Position = 0;
string contentType;
if (!new FileExtensionContentTypeProvider().TryGetContentType(file.FileName, out contentType))
{
contentType = "application/octet-stream";
}
Response.Headers.Add(HeaderNames.ContentDisposition, new ContentDisposition
{
FileName = file.FileName,
Inline = true,
}.ToString());
return File(memory, contentType, true);
}
Response.StatusCode = 404;
return null;
}
这终于奏效了,我在浏览器中获得了一个播放良好的可搜索视频。如果下载速度很慢,并且似乎没有完全尊重来自浏览器的范围标头,那么我现在的问题是。
这是来自 Safari 的网络跟踪:
上面确实感觉文件从 Azure 下载了 3 次,尽管数据返回到浏览器;这将使它变得缓慢和低效......
我还注意到我可能想要的解决方案来自HttpRange
此方法中的参数:Download(HttpRange, BlobRequestConditions, Boolean, CancellationToken)
我的问题是,如果我使用 Download 方法,我无法获得可搜索的响应,我可以让它工作的唯一方法是使用DownloadTo(stream)
,如上所述。
最好的方法是什么...?
非常感谢!
解决方案
推荐阅读
- java - 如何使用 Vaadin 将图像上传到画布?
- apache-spark - 在 s3 上将 Df 写入镶木地板文件的最快方法
- jquery - 如何在 jQuery 中打开 android 上的键盘?
- yii2 - Yii2搜索表单没有重新加载页面
- excel - 有没有办法使用 PowerShell 仅对宏(在 Excel、Word 等文件中)进行数字签名?
- java - 将 mongo 集合复制到相关的 java 对象中
- html - 正则表达式:捕获不包含正则表达式的组
- laravel-4 - 如何将两个不同的 id 从 onclick 函数发送到资源路由控制器
- javascript - 在模式弹出窗口中播放嵌入的 YouTube 视频?
- html - 选择选项选定的关键字不起作用