Export (0) Print
Expand All

CloudBlob.DownloadToFile Method (String, BlobRequestOptions)

Updated: April 24, 2013

[This topic is part of the Microsoft Azure Storage Client Library 1.7, which has been deprecated. See Storage Client Library for the latest version.]

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
Dim instance As CloudBlob
Dim fileName As String
Dim options As BlobRequestOptions

instance.DownloadToFile(fileName, options)

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, Windows Server 2008, Windows 8.1, Windows Server 2012 R2, Windows 8 and Windows Server 2012

 

Show:
© 2014 Microsoft