Tuesday, June 8, 2010

CloudBlockBlob.DownloadToFile()

I've been using CloudBlockBlob.DownloadToFile() without a hitch for the last couple of months but I started running parts of the project locally to do some debugging and it looks like DownloadToFile has a bit of a problem with it.  When I try DownloadToFile on a CloudBlockBlob that isn't small small (it's actually 64MB), I get a System.IO.IOException: "Unable to read data from the transport connection: The connection was closed."

Looks like you need to use the stream version:

// This will override anything that already exists,

using (FileStream fs = new FileStream(filename, FileMode.Create))
{

// I'm using a one-hour timeout

fileBlob.DownloadToStream(fs, new BlobRequestOptions() { Timeout = new TimeSpan(1, 0, 0)});

}



Update: I did a bit of testing with the Timeout BlobRequestOptions and the TimeSpan you set is more of a guideline.. ;)  I set the timeout to be 1 second and it took about 10 seconds to actually timeout.  I'm guessing that they stream a certain amount (I think it was 4096kb) then check if the timeout has passed, then read a bit more if it hasn't.  In any case, it's working now.


Update 2: Stupidly I wasn't closing the file stream which caused the file to be cut short!  argh!  Anyway, wrap it in a "using" like usual to call the Close().

No comments:

Post a Comment