In the previous article in this series, I showed you how to use the Storage Client Library to do many of the operations needed to manage files in blob storage, such as upload, download, copy, delete, list, and rename. The CloudBlockBlob.UploadFile works fine, but it can be tuned for special cases such as very slow internet access.
When I worked for a startup, one of the things our desktop product did was upload a bunch of images and an MP3 file to Azure blob storage. The MP3 could be as large as 20 MB. Many of our customers lived in areas with broadband upload speeds of 1.0 mbps on a good day. When we tested using File.Upload on a 20MB file with minimal broadband speed, we found the upload would time out and eventually fail. It just couldnt send up enough bytes and get a handshake back quickly enough to be successful.
In order to make our product work for all customers, we changed the upload to send the file up in blocks. The customer could set the block size. If the customer had pretty good internet speed (5 mbps or higher), they might set the block size as high as 1 MB. If they had pretty bad internet speed (1 mbps or lower), they could set the block size as low as 256kb. This is small enough for a block to be uploaded and the handshake completed, and then it could start on the next block.
In this article, Im going to discuss two ways to upload a file in blocks. One way is to use the parameters that can be changed when calling the UploadFile method on the CloudBlockBlob object. The other way is to programmatically break the file into blocks and upload them one by one, then ask Azure to reassemble them.
Lets start with using the built-in functions for uploading a file. I messed around with this a bit back in 2010-2011, but the properties as used back then are obsolete, and/or have been moved to different objects of the Storage Client Library since then. Bing-ing SingleBlobUploadThresholdInBytes only returned 8 articles. (Think about that. What have you searched for lately that only returned 8 results?) Most of the articles were from 2010-2011; the others were from MSDN, which offered a useful explanation like this: This is the threshold in bytes for a single blob upload. Wow, incredibly helpful.
I managed to track down someone on the Azure Storage team at Microsoft to help me understand this, so at the time of this writing, I think only three people in the world know how to use this correctly me, the guy at Microsoft who owns it, and one of the other Azure MVPs. So after you read this, you will be part of a very elite group.
There are three properties directly involved.
This is the threshold in bytes for a single blob upload. (Haha! Kidding!) This setting determines whether the blob will be uploaded in one shot (Put Blob) or multiple requests (Put Block). It does not determine the block size. It basically says if the file is smaller than this size, upload it as one block. If the file size is larger than this value, break it into blocks and upload it.
The minimum value for this is 1MB (1024 * 1024). This means you can not use this to chunk files that are smaller than 1 MB. ParallelOperationThreadCount must be equal to 1 (more on that below). Also, this works with the Upload* APIs (such as UploadFile) but not to blob streams. If you use OpenWrite to get a stream and write to it, it will always be uploaded behind the scenes using Put Block calls.
This property is found in the BlobRequestOptions class. To use it, create a BlobRequestOptions object and then assign it to the CloudBlobClients DefaultRequestOptions property.
This sets the size of the blocks to use when you do a Put Blob and it breaks it into blocks to upload because the file is larger than the value of SingleBlobUploadThresholdInBytes.
By default, this is 4MB (4 * 1024 * 1024).
This is a property on the CloudBlockBlob object or CloudPageBlob object, whichever you are using. You can use this when streaming files up to Azure as well (like when youre using UploadStream instead of UploadFile).
This specifies how many parallel PutBlock or PutPage operations should be pending at a time.
If this is set to anything but 1, SingleBlobUploadThresholdInBytes will be ignored. After all, if you ask the file to be sent up in multiple threads, theres no way to do that but to send it up in blocks, right?
This is a property of the BlobRequestOptions object.
So for example, if you use these values:
and call blob.UploadFile, if the file is less than 1MB, it will use one Put Blob to upload it. If the file is larger than 1 MB, it will split it into 256kb blocks and send the blocks up as multiple requests.
You might also consider changing the default Retry Policy. If youre chunking the file because you think the client will have problems uploading it because their internet connection is poor, you might want to set it to only retry once, or not at all. Otherwise it may time out, then wait X seconds and time out again, etc, when it will never succeed. For this reason, Im only having it retry once in the code below.
Uploading a file using the .NET Storage SDK
TimeSpan backOffPeriod = TimeSpan.FromSeconds(2);
int retryCount = 1;
BlobRequestOptions bro = new BlobRequestOptions()
{
SingleBlobUploadThresholdInBytes = 1024 * 1024, //1MB, the minimum
ParallelOperationThreadCount = 1,
RetryPolicy = new ExponentialRetry(backOffPeriod, retryCount),
};
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
cloudBlobClient.DefaultRequestOptions = bro;
cloudBlobContainer = cloudBlobClient.GetContainerReference(ContainerName);
CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(Path.GetFileName(fileName));
blob.StreamWriteSizeInBytes = 256 * 1024; //256 k
blob.UploadFromFile(fileName, FileMode.Open);
In the code above, you can see that I create a BlobRequestOptions object, assign the values of SingleBlobUploadThresholdInBytes, ParallelOperationThreadCount, and RetryPolicy. Then after instantiating the CloudBlobClient, I set the DefaultRequestOptions to my BlobRequestOptions object. After getting a reference to the blob, I set the StreamWriteSizeInBytes. Then I upload the file.
If I turn fiddler on and use the code above to upload a 5MB file, I see multiple requests one for each block. These calls are made consecutively because they are all running in a single thread (ParallelOperationThreadCount = 1).
Figure 1: Fiddler View
And if I look at any one line, I can see the size of the request. For all but the last two, the block size is the same as StreamWriteSizeInBytes. The last two send out the remainder of the blocks.
Figure 2: Fiddler Details
If you can set a couple of properties and upload a file in blocks easily, why would you want to do it programmatically? The case that immediately comes to mind is if you have files that are less than 1 MB and you want to send them up in 256kb blocks. The minimum value for SingleBlobUploadThresholdInBytes is 1 MB, so you can not use the method above.
Another case is if you want to let the user pause the upload process, then come back later and restart it. Ill talk about this after the code for uploading a file in blocks.
To programmatically upload a file in blocks, you first open a file stream for the file. Then repeatedly read a block of the file, set a block ID, calculate the MD5 hash of the block and write the block to blob storage. Keep a list of the block IDs as you go. When youre done, you call PutBlockList and pass it the list of block IDs. Azure will put the blocks together in the order specified in the list, and then commit them. If you get the Block List out of order, or you dont put all of the blocks before committing the list, your file will be corrupted.
The block ids must all be the same size for all of the blocks, or your upload/commit will fail. I usually just number them from 1 to whatever, using a block ID that is formatted to a 7-character string. So for 1, Ill get 0000001. Note that block ids have to be a base 64 string.
Heres the code for uploading a file in blocks. Ive put comments in to explain whats going on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Link:
Azure Blob Storage Part 4: Uploading Large Blobs - Simple Talk
- Mind uploading won't lead to immortality - Life 2.0 ... [Last Updated On: June 10th, 2016] [Originally Added On: June 10th, 2016]
- Mind uploading won't lead to immortality - Life 2.0 ... [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Mind uploading in fiction - Wikipedia, the free encyclopedia [Last Updated On: June 12th, 2016] [Originally Added On: June 12th, 2016]
- Mind Uploading [Last Updated On: June 21st, 2016] [Originally Added On: June 21st, 2016]
- Trasferimento della mente - Wikipedia [Last Updated On: June 28th, 2016] [Originally Added On: June 28th, 2016]
- Mind Uploading FAQs [Last Updated On: June 30th, 2016] [Originally Added On: June 30th, 2016]
- Mind uploading - RationalWiki [Last Updated On: June 30th, 2016] [Originally Added On: June 30th, 2016]
- Brain Uploading - TV Tropes [Last Updated On: July 1st, 2016] [Originally Added On: July 1st, 2016]
- Carboncopies.org Foundation [Last Updated On: July 1st, 2016] [Originally Added On: July 1st, 2016]
- The History of SIM, Whole Brain Emulation and Mind Uploading [Last Updated On: July 31st, 2016] [Originally Added On: July 31st, 2016]
- What are the Benefits of Mind Uploading? - Lifeboat [Last Updated On: November 12th, 2016] [Originally Added On: November 12th, 2016]
- 2100 Technology | Timeline | 22nd Century | Future ... [Last Updated On: November 21st, 2016] [Originally Added On: November 21st, 2016]
- Unsterblichkeit Wikipedia [Last Updated On: November 23rd, 2016] [Originally Added On: November 23rd, 2016]
- Mind uploading - Transhumanism Wiki - Wikia [Last Updated On: December 2nd, 2016] [Originally Added On: December 2nd, 2016]
- Make Money from Images, Documents and Photos Uploading [Last Updated On: December 7th, 2016] [Originally Added On: December 7th, 2016]
- Immortal but Damned to Hell on Earth - The Atlantic [Last Updated On: January 29th, 2017] [Originally Added On: January 29th, 2017]
- Faultlines, black holes and glaciers: mapping uncharted territories - The Guardian [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- How to keep your children safe online as it's revealed half of six-year-olds use the internet - Mirror.co.uk [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Yetunde Olasiyan: Between Having a Voice & the Need to Show Off on Social Media - Bella Naija [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- How a WiFi Pilot Program Is Helping Students in the Rio Grande Valley - Texas Public Radio [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Looking for a New Job? 4 Ways to Job Search Discreetly - U.S. News & World Report (blog) [Last Updated On: February 7th, 2017] [Originally Added On: February 7th, 2017]
- Khloe Kardashian narrowly avoids a wardrobe malfunction in sexy underwear photo - OK! Magazine [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- How a WiFi Pilot Program Is Helping Students in the Rio Grande Valley - KWBU [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- Connectivity in a digital world - Iowa City Press Citizen [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- Does Your Vision for Your Life Cause You to Look Up...or Down? - Huffington Post [Last Updated On: February 9th, 2017] [Originally Added On: February 9th, 2017]
- How to improve your LinkedIn profile - ArabianBusiness.com [Last Updated On: February 10th, 2017] [Originally Added On: February 10th, 2017]
- How a WiFi Pilot Program Is Helping Students in the Rio Grande Valley - KUT [Last Updated On: February 10th, 2017] [Originally Added On: February 10th, 2017]
- Deal: New customers can get Google Play Music and YouTube Red free for 4 months - Android Authority (blog) [Last Updated On: February 11th, 2017] [Originally Added On: February 11th, 2017]
- Procurious and SOPO Launch Innovative Online Community Platform for Government Procurement Professionals - PR Newswire UK (press release) [Last Updated On: February 13th, 2017] [Originally Added On: February 13th, 2017]
- Breaking Down Global Silos (Part 2): Lessons Learned from Conflict - Spend Matters [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- Robbie Williams' wife Ayda proves she's the best ever as she completely overshares with hilarious birthday card to him - The Sun [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- Morality and Murder Collide in Two New Horror Movies - Film School Rejects [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- Jefferson schools urged to use cable TV station - Daily Union [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- Olly Murs sings Happy Birthday to Robbie Williams with full backing band in sweet video - The Sun [Last Updated On: February 14th, 2017] [Originally Added On: February 14th, 2017]
- Eric Adler Is Living The Dream With The New England Patriots - Cape Cod Chronicle [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- Squash review: Drop and compress your way to smaller image files in a flash - Macworld [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- It's time to get tech-savvy with The Mind Lab by Unitec! - Scoop.co.nz [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- Tennyson soundtracks movies for your mind with Like What - Straight.com [Last Updated On: February 15th, 2017] [Originally Added On: February 15th, 2017]
- Fake news, who benefits? - Shelbyville Times-Gazette (blog) [Last Updated On: February 17th, 2017] [Originally Added On: February 17th, 2017]
- Crazy Videos on YouTube That No One Can Explain - Thrillist [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- Barbie becomes a hologram version of herself - TechCrunch [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- The Owl at the Window review: They the living - Irish Times [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- Tax Software: The Basics Work, but Peace of Mind Costs Extra - New York Times [Last Updated On: February 18th, 2017] [Originally Added On: February 18th, 2017]
- How To Run A Rogue Government Twitter Account With An Anonymous Email Address And A Burner Phone - UPROXX [Last Updated On: February 20th, 2017] [Originally Added On: February 20th, 2017]
- The three reasons YouTubers keep imploding, from a YouTuber - Polygon [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- SnailBlitz 2017: Citizen Scientists Wanted - NBC Southern California [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- Breakdown In Immigration Screening: The Devil Is In The Details - Daily Caller [Last Updated On: February 22nd, 2017] [Originally Added On: February 22nd, 2017]
- Appealing Social Security Decisions Online - Danbury News Times [Last Updated On: February 26th, 2017] [Originally Added On: February 26th, 2017]
- Nikon D5600 Review: Hoping to Make Photo Transfers a Snap - Huffington Post [Last Updated On: February 26th, 2017] [Originally Added On: February 26th, 2017]
- How to file your social security appeal online - KARE [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- Comparison: 3 Platforms Paying You in Bitcoin for Uploading Images - Nigeria Today [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- How to file your social security appeal online - WZZM13.com [Last Updated On: February 28th, 2017] [Originally Added On: February 28th, 2017]
- How To File Your Social Security Appeal Online - WFMYNews2.com [Last Updated On: March 1st, 2017] [Originally Added On: March 1st, 2017]
- B2B processes get smarter in the cloud - TechTarget [Last Updated On: March 2nd, 2017] [Originally Added On: March 2nd, 2017]
- Data limits are the worsthere's how to stay under yours - Popular Science [Last Updated On: March 2nd, 2017] [Originally Added On: March 2nd, 2017]
- Overcome problems with public cloud storage providers - TechTarget [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- From AI to Anxiety Relief, The Brain Needs a Body - Big Think [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- The Modest Problem of Death: On Mark O'Connell's To Be a Machine - lareviewofbooks [Last Updated On: March 4th, 2017] [Originally Added On: March 4th, 2017]
- Mind the gap: burn and destroy mission in Bengal fields leaves a few questions - Hindustan Times [Last Updated On: March 5th, 2017] [Originally Added On: March 5th, 2017]
- A man with vitiligo who was called 'zebra' by bullies has defied their cruel comments by becoming a model - The Sun [Last Updated On: March 8th, 2017] [Originally Added On: March 8th, 2017]
- Cam Site Launches iTunes-Like Marketplace Of Oral Sex For Women - AskMen [Last Updated On: March 10th, 2017] [Originally Added On: March 10th, 2017]
- Everything new in Stellaris: Utopia, one of Paradox's biggest game ... - PC Gamer [Last Updated On: March 10th, 2017] [Originally Added On: March 10th, 2017]
- Paytm to continue free uploading of money - Business Standard [Last Updated On: March 11th, 2017] [Originally Added On: March 11th, 2017]
- Paytm to continue free uploading of money | Free Press Journal - Free Press Journal [Last Updated On: March 11th, 2017] [Originally Added On: March 11th, 2017]
- How to upload photos to Instagram from a PC: Upload your favourite images to Instagram on Windows 10 without a ... - PC Advisor [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- 2017 Foresight Fellows Announced in Molecular Machines, Space, Longevity, Artificial Intelligence - Benzinga [Last Updated On: March 17th, 2017] [Originally Added On: March 17th, 2017]
- Best Practices for 2017 SEO Audits [PODCAST] - Search Engine Journal [Last Updated On: March 23rd, 2017] [Originally Added On: March 23rd, 2017]
- Science to Beat the Death: 200 300 years old human in the Future! - Sri Lanka Guardian [Last Updated On: March 23rd, 2017] [Originally Added On: March 23rd, 2017]
- Microsoft's Windows 10 Creators Update lives up to its name - Engadget [Last Updated On: March 29th, 2017] [Originally Added On: March 29th, 2017]
- Film reviews: Ghost In The Shell and other releases - Herald Scotland [Last Updated On: April 2nd, 2017] [Originally Added On: April 2nd, 2017]
- Death of a Dystopian - The New Yorker [Last Updated On: April 3rd, 2017] [Originally Added On: April 3rd, 2017]
- Stealth Rick and Morty premiere a delightful surprise - Marquette Wire [Last Updated On: April 3rd, 2017] [Originally Added On: April 3rd, 2017]
- Severe Weather Risk on Wednesday - WEAR [Last Updated On: April 5th, 2017] [Originally Added On: April 5th, 2017]
- Showtime docu-series sees the 'Dark' side of tech - LA Daily News [Last Updated On: April 5th, 2017] [Originally Added On: April 5th, 2017]
- Report It - Maui Now [Last Updated On: April 7th, 2017] [Originally Added On: April 7th, 2017]
- The perils and false rewards of parenting in the era of 'digi-discipline' - Minnesota Public Radio News [Last Updated On: April 7th, 2017] [Originally Added On: April 7th, 2017]
- Elon Musk: Australian man pens desperate letter to download his brain - NEWS.com.au [Last Updated On: April 7th, 2017] [Originally Added On: April 7th, 2017]
- Police body cameras part of Dothan's new integrated system - Dothan Eagle [Last Updated On: April 10th, 2017] [Originally Added On: April 10th, 2017]
- This Is the Dawn of Brain Tech, But How Far Can It Go? - Singularity Hub [Last Updated On: April 12th, 2017] [Originally Added On: April 12th, 2017]
- 7 easy ways to make your iPhone videos look pro - CNET [Last Updated On: April 13th, 2017] [Originally Added On: April 13th, 2017]