This article shows how to upload a blob using the Azure Storage client library for .NET. You can upload data to a block blob from a file path, a stream, a binary object, or a text string. You can also open a blob stream and write to it, or upload large blobs in blocks.
Latest .NET SDK for your operating system. Be sure to get the SDK and not the runtime.
Set up your environment
If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for .NET. The steps include package installation, adding using directives, and creating an authorized client object. For details, see Get started with Azure Blob Storage and .NET.
Install packages
From your project directory, install packages for the Azure Blob Storage and Azure Identity client libraries using the dotnet add package command. The Azure.Identity package is needed for passwordless connections to Azure services.
Add these using directives to the top of your code file:
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
Some code examples in this article might require additional using directives.
Create a client object
To connect an app to Blob Storage, create an instance of BlobServiceClient. The following example shows how to create a client object using DefaultAzureCredential for authorization:
public BlobServiceClient GetBlobServiceClient(string accountName)
{
BlobServiceClient client = new(
new Uri($"https://{accountName}.blob.core.windows.net"),
new DefaultAzureCredential());
return client;
}
The authorization mechanism must have the necessary permissions to upload a blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Put Blob (REST API) and Put Block (REST API).
Upload data to a block blob
You can use either of the following methods to upload data to a block blob:
When using these upload methods, the client library may call either Put Blob or a series of Put Block calls followed by Put Block List. This behavior depends on the overall size of the object and how the data transfer options are set.
To open a stream in Blob Storage and write to that stream, use either of the following methods:
You can open a stream in Blob Storage and write to it. The following example creates a zip file in Blob Storage and writes files to it. Instead of building a zip file in local memory, only one file at a time is in memory.
public static async Task UploadToStreamAsync(
BlobContainerClient containerClient,
string localDirectoryPath)
{
string zipFileName = Path.GetFileName(
Path.GetDirectoryName(localDirectoryPath)) + ".zip";
BlockBlobClient blockBlobClient = containerClient.GetBlockBlobClient(zipFileName);
using (Stream stream = await blockBlobClient.OpenWriteAsync(true))
{
using (ZipArchive zip = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: false))
{
foreach (var fileName in Directory.EnumerateFiles(localDirectoryPath))
{
using (var fileStream = File.OpenRead(fileName))
{
var entry = zip.CreateEntry(
Path.GetFileName(fileName), CompressionLevel.Optimal);
using (var innerFile = entry.Open())
{
await fileStream.CopyToAsync(innerFile);
}
}
}
}
}
}
Upload a block blob with configuration options
You can define client library configuration options when uploading a blob. These options can be tuned to improve performance, enhance reliability, and optimize costs. The following code examples show how to use BlobUploadOptions to define configuration options when calling an upload method.
Specify data transfer options on upload
You can configure the values in StorageTransferOptions to improve performance for data transfer operations. The following code example shows how to set values for StorageTransferOptions and include the options as part of a BlobUploadOptions instance. The values provided in this sample aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app.
public static async Task UploadWithTransferOptionsAsync(
BlobContainerClient containerClient,
string localFilePath)
{
string fileName = Path.GetFileName(localFilePath);
BlobClient blobClient = containerClient.GetBlobClient(fileName);
var transferOptions = new StorageTransferOptions
{
// Set the maximum number of parallel transfer workers
MaximumConcurrency = 2,
// Set the initial transfer length to 8 MiB
InitialTransferSize = 8 * 1024 * 1024,
// Set the maximum length of a transfer to 4 MiB
MaximumTransferSize = 4 * 1024 * 1024
};
var uploadOptions = new BlobUploadOptions()
{
TransferOptions = transferOptions
};
FileStream fileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(fileStream, uploadOptions);
fileStream.Close();
}
You can specify transfer validation options to help ensure that data is uploaded properly and hasn't been tampered with during transit. Transfer validation options can be defined at the client level using BlobClientOptions, which applies validation options to all methods called from a BlobClient instance.
You can also override transfer validation options at the method level using BlobUploadOptions. The following code example shows how to create a BlobUploadOptions object and specify an algorithm for generating a checksum. The checksum is then used by the service to verify data integrity of the uploaded content.
public static async Task UploadWithChecksumAsync(
BlobContainerClient containerClient,
string localFilePath)
{
string fileName = Path.GetFileName(localFilePath);
BlobClient blobClient = containerClient.GetBlobClient(fileName);
var validationOptions = new UploadTransferValidationOptions
{
ChecksumAlgorithm = StorageChecksumAlgorithm.Auto
};
var uploadOptions = new BlobUploadOptions()
{
TransferValidation = validationOptions
};
FileStream fileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(fileStream, uploadOptions);
fileStream.Close();
}
The following table shows the available options for the checksum algorithm, as defined by StorageChecksumAlgorithm:
Name
Value
Description
Auto
0
Recommended. Allows the library to choose an algorithm. Different library versions may choose different algorithms.
None
1
No selected algorithm. Don't calculate or request checksums.
MD5
2
Standard MD5 hash algorithm.
StorageCrc64
3
Azure Storage custom 64-bit CRC.
Note
If the checksum specified in the request doesn't match the checksum calculated by the service, the upload operation fails. The operation is not retried when using a default retry policy. In .NET, a RequestFailedException is thrown with status code 400 and error code Md5Mismatch or Crc64Mismatch, depending on which algorithm is used.
Upload with index tags
Blob index tags categorize data in your storage account using key-value tag attributes. These tags are automatically indexed and exposed as a searchable multi-dimensional index to easily find data. You can add tags to a BlobUploadOptions instance, and pass that instance into the UploadAsync method.
The following example uploads a block blob with index tags:
You can set a blob's access tier on upload by using the BlobUploadOptions class. The following code example shows how to set the access tier when uploading a blob:
Setting the access tier is only allowed for block blobs. You can set the access tier for a block blob to Hot, Cool, Cold, or Archive. To set the access tier to Cold, you must use a minimum client library version of 12.15.0.
Upload a block blob by staging blocks and committing
You can have greater control over how to divide uploads into blocks by manually staging individual blocks of data. When all of the blocks that make up a blob are staged, you can commit them to Blob Storage. You can use this approach to enhance performance by uploading blocks in parallel.
The Azure SDK for .NET contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar .NET paradigms. The client library methods for uploading blobs use the following REST API operations:
This article is part of the Blob Storage developer guide for .NET. To learn more, see the full list of developer guide articles at Build your .NET app.
Build end-to-end solutions in Microsoft Azure to create Azure Functions, implement and manage web apps, develop solutions utilizing Azure storage, and more.