简体   繁体   中英

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.

In the example, the first controller action creates a blob reference and stores some meta data in the 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?

In case it affects the answer, I'd like to call WebAPI controller from javascript, so there's no session anyway.

You have a couple options. The first would be to continue to use the Session object and change the Session Provider (see more below). The second would be to write your own layer that would handle caching for you to something such as Redis. The currently recommended caching solution in either case is 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
  • Sql Server Session State Provider - This would have a impact on performance as it would make round trips to the SQL database.
  • Distributed In Memory Session State Provider such as Redis Cache Session State Provider - This is the currently recommended solution for using the Session State .

Using the Redis Session State Provider

You can continue to use the Session object and switch the Session Provider in the Web.Config to use Redis instead of in memory. First add the RedisSessionStateProvider Package from NuGet then update the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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