SALES: 1-800-867-1380
This topic has not yet been rated - Rate this topic

CloudBlob.DownloadToFile Method (String, BlobRequestOptions)

Updated: April 24, 2013

[This topic is part of the Windows Azure Storage Client Library 1.7, which has been deprecated. The current recommended version is Storage Client Library 3.0.]

Downloads the blob's contents to a file, using a conditional request based on the BlobRequestOptions that you specify.

Namespace: Microsoft.WindowsAzure.StorageClient
Assembly: Microsoft.WindowsAzure.StorageClient (in Microsoft.WindowsAzure.StorageClient.dll)
'Usage

public void DownloadToFile (
	string fileName,
	BlobRequestOptions options
)

Parameters

fileName

Type: System.String

The path and file name of the target file.

options

Type: Microsoft.WindowsAzure.StorageClient.BlobRequestOptions

An object that specifies any additional options for the request.

The following code example downloads a blob to a local file only if the blob has been modified since January 1, 2012.

static void DownloadBlobToFile(Uri blobEndpoint, string accountName, string accountKey)
{
    // Create service client for credentialed access to the Blob service.
    CloudBlobClient blobClient = 
        new CloudBlobClient(blobEndpoint, new StorageCredentialsAccountAndKey(accountName, accountKey));

    // Return a reference to the blob.
    CloudBlob blob = blobClient.GetBlobReference("mycontainer/myblob");

    // Download the blob to a local file only if it has been modified since a fixed date.
    DateTime dt = new DateTime(2012, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    try
    {
        blob.DownloadToFile("c:\\temp\\mylocalblob.txt", new BlobRequestOptions()
                {
                    AccessCondition = AccessCondition.IfModifiedSince(dt.ToUniversalTime())
                });
    }
    catch (StorageClientException e)
    {
        if (e.StatusCode == System.Net.HttpStatusCode.NotModified)
            Console.WriteLine("The blob was not modified since January 1, 2012.");
        else
            throw e; // some other StorageClientException, re-throw
    }
}

This method downloads a blob to a file in the local file system. If the file does not exist, it is created; if it does exist, it is overwritten.

WarningWarning
If you download a blob to an existing file and the size of the blob is less than that size of the file, the file will not be completely overwritten and data corruption may occur. This is a known bug in the client library. It's recommended that you delete the existing file on disk before downloading the blob.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Development Platforms

Windows Vista, Windows 7 and Windows Server 2008

Target Platforms

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.