简体   繁体   English

Azure Blob存储-分块文件上传-跨回发缓存数据

[英]Azure blob storage - chunked file upload - caching data across postbacks

I've been following this example for uploading large files from an MVC web app to Azure blob storage in chunks. 我一直在遵循这个示例 ,将大文件从MVC Web应用程序上传到块中的Azure blob存储。

In the example, the first controller action creates a blob reference and stores some meta data in the Session: 在示例中,第一个控制器操作创建一个Blob引用,并将一些元数据存储在Session中:

        var fileToUpload = new CloudFile()
        {
            BlockCount = blocksCount,
            FileName = fileName,
            Size = fileSize,
            BlockBlob = container.GetBlockBlobReference(fileName),
            StartTime = DateTime.Now,
            IsUploadCompleted = false,
            UploadStatusMessage = string.Empty
        };
        Session.Add("CurrentFile", fileToUpload);

to allow each successive call to pick up where it left off: 以允许每个后续呼叫在中断的地方接听:

 CloudFile model = (CloudFile)Session["CurrentFile"];
 model.BlockBlob.PutBlock(*new chunk stream*);

It's obvious this was done for convenience in the tutorial, but not obvious to me how it should be done. 显然,在本教程中这样做是为了方便起见,但对我而言,不应该怎么做。 For a scalable cloud application I don't want to be using session at all. 对于可伸缩的云应用程序,我根本不想使用会话。

My question is, would it be perfectly fine to simply commit and rewrite to blob storage on every chunk upload, and if not, is there a suitable caching alternative for Azure applications? 我的问题是,在每个块上载时简单地提交并重写到Blob存储是否会很好?如果不是,是否有适合Azure应用程序的缓存替代方法?

In case it affects the answer, I'd like to call WebAPI controller from javascript, so there's no session anyway. 如果它影响答案,我想从javascript调用WebAPI控制器,因此无论如何都没有会话。

You have a couple options. 您有两种选择。 The first would be to continue to use the Session object and change the Session Provider (see more below). 第一种是继续使用Session对象并更改Session Provider (请参阅下文)。 The second would be to write your own layer that would handle caching for you to something such as Redis. 第二步是编写您自己的层,该层将为处理诸如Redis之类的缓存 The currently recommended caching solution in either case is Redis . 无论哪种情况, 当前推荐的缓存解决方案都是Redis

For the first option there are several Session Providers available : 对于第一种选择,有几个可用的会话提供程序

  • In Memory Session State Provider - Defualt but as you mentioned doesn't scale well 在“内存会话状态提供程序”中-Defualt,但正如您提到的那样,扩展性不好
  • Sql Server Session State Provider - This would have a impact on performance as it would make round trips to the SQL database. Sql Server会话状态提供程序-这将对性能产生影响,因为它将往返SQL数据库。
  • Distributed In Memory Session State Provider such as Redis Cache Session State Provider - This is the currently recommended solution for using the Session State . 分布式内存会话状态提供程序,例如Redis缓存会话状态提供程序- 这是当前推荐的使用会话状态的解决方案

Using the Redis Session State Provider 使用Redis会话状态提供程序

You can continue to use the Session object and switch the Session Provider in the Web.Config to use Redis instead of in memory. 您可以继续使用Session对象,并在Web.Config中将Session Provider切换为使用Redis而不是在内存中。 First add the RedisSessionStateProvider Package from NuGet then update the web.config: 首先从NuGet添加RedisSessionStateProvider软件包,然后更新web.config:

<sessionStatemode="Custom" customProvider="MySessionStateStore">
<providers>
<!--Remove old session state info if currently configured.-->
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="<redis host url/ip here>" accessKey="<your access key here>" />
</providers>

This article on caching guidance in Azure by the Microsoft Patterns and Practices Team has tons of information on both scenarios. Microsoft模式和实践团队撰写的有关Azure中缓存指导的文章包含有关这两种情况的大量信息。

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

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