private void DoUploadDownloadFile(ICloudBlob blob, int fileSize, bool isAsync)
        {
            string inputFileName = Path.GetTempFileName();
            string outputFileName = Path.GetTempFileName();

            try
            {
                byte[] buffer = GetRandomBuffer(fileSize);
                using (FileStream file = new FileStream(inputFileName, FileMode.Create, FileAccess.Write))
                {
                    file.Write(buffer, 0, buffer.Length);
                }

                if (isAsync)
                {
                    IAsyncResult result;
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        OperationContext context = new OperationContext();
                        result = blob.BeginUploadFromFile(inputFileName,
                                ar => waitHandle.Set(),
                                null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromFile(result);

                        result = blob.BeginUploadFromFile(inputFileName, null, null, context,
                            ar => waitHandle.Set(),
                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromFile(result);
                        Assert.IsNotNull(context.LastResult.ServiceRequestID);

                        TestHelper.ExpectedException<IOException>(
                            () => blob.BeginDownloadToFile(outputFileName, FileMode.CreateNew, null, null),
                            "CreateNew on an existing file should fail");

                        context = new OperationContext();
                        result = blob.BeginDownloadToFile(outputFileName, FileMode.Create, null, null, context,
                            ar => waitHandle.Set(),
                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToFile(result);
                        Assert.IsNotNull(context.LastResult.ServiceRequestID);

                        using (FileStream inputFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
                            outputFile = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
                        {
                            TestHelper.AssertStreamsAreEqual(inputFile, outputFile);
                        }

                        result = blob.BeginDownloadToFile(outputFileName, FileMode.Append,
                            ar => waitHandle.Set(),
                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToFile(result);

                        using (FileStream inputFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
                            outputFile = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
                        {
                            Assert.AreEqual(2 * fileSize, outputFile.Length);

                            for (int i = 0; i < fileSize; i++)
                            {
                                Assert.AreEqual(inputFile.ReadByte(), outputFile.ReadByte());
                            }

                            inputFile.Seek(0, SeekOrigin.Begin);
                            for (int i = 0; i < fileSize; i++)
                            {
                                Assert.AreEqual(inputFile.ReadByte(), outputFile.ReadByte());
                            }
                        }
                    }
                }
                else
                {
                    OperationContext context = new OperationContext();

                    blob.UploadFromFile(inputFileName);
                    blob.UploadFromFile(inputFileName, null, null, context);
                    Assert.IsNotNull(context.LastResult.ServiceRequestID);

                    TestHelper.ExpectedException<IOException>(
                        () => blob.DownloadToFile(outputFileName, FileMode.CreateNew),
                        "CreateNew on an existing file should fail");

                    context = new OperationContext();
                    blob.DownloadToFile(outputFileName, FileMode.Create, null, null, context);
                    Assert.IsNotNull(context.LastResult.ServiceRequestID);

                    using (FileStream inputFileStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
                        outputFileStream = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
                    {
                        TestHelper.AssertStreamsAreEqual(inputFileStream, outputFileStream);
                    }

                    blob.DownloadToFile(outputFileName, FileMode.Append);

                    using (FileStream inputFileStream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read),
                        outputFileStream = new FileStream(outputFileName, FileMode.Open, FileAccess.Read))
                    {
                        Assert.AreEqual(2 * fileSize, outputFileStream.Length);

                        for (int i = 0; i < fileSize; i++)
                        {
                            Assert.AreEqual(inputFileStream.ReadByte(), outputFileStream.ReadByte());
                        }

                        inputFileStream.Seek(0, SeekOrigin.Begin);
                        for (int i = 0; i < fileSize; i++)
                        {
                            Assert.AreEqual(inputFileStream.ReadByte(), outputFileStream.ReadByte());
                        }
                    }
                }
            }
            finally
            {
                File.Delete(inputFileName);
                File.Delete(outputFileName);
            }
        }