Upload & Download Files in Azure Blob Storage
Overview
Learn how to upload and download files to Azure Blob Storage using the Azure Portal, CLI, and .NET SDK.
Method 1: Azure Portal
Upload a File
- Navigate to your Storage Account
- Go to Data storage → Containers
- Select a container (or create one)
- Click Upload button
- Select your file(s) and click Upload
Download a File
- Navigate to the container
- Click on the blob (file)
- Click Download button
Method 2: Azure CLI
Install Azure CLI
# Windows (using winget)
winget install Microsoft.AzureCLI
# macOS
brew install azure-cli
# Linux
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Login and Upload
# Login
az login
# Upload a file
az storage blob upload \
--account-name mystorage001 \
--container-name mycontainer \
--name myfile.txt \
--file myfile.txt
# Upload a folder
az storage blob upload-batch \
--source ./myfolder \
--destination mycontainer
Download a File
# Download a file
az storage blob download \
--account-name mystorage001 \
--container-name mycontainer \
--name myfile.txt \
--file ./downloaded.txt
Method 3: .NET SDK
Install Package
dotnet add package Azure.Storage.Blobs
Upload Code
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
string connectionString = "DefaultEndpointsProtocol=https;AccountName=mystorage001;AccountKey=...";
string containerName = "mycontainer";
string filePath = "myfile.txt";
var blobServiceClient = new BlobServiceClient(connectionString);
var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
await blobContainerClient.CreateIfNotExistsAsync();
var blobClient = blobContainerClient.GetBlobClient("myfile.txt");
await blobClient.UploadAsync(filePath, overwrite: true);
Download Code
var blobClient = blobContainerClient.GetBlobClient("myfile.txt");
await blobClient.DownloadToAsync("downloaded.txt");
Best Practices
- Use Block Blobs for most file storage
- Enable Soft Delete to protect against accidental deletion
- Use Parallel Transfers for large files
- Set Content-Type properly for browser rendering
Real-Time Scenarios
Scenario 1: User File Upload Handler (Web Application)
A typical web app pattern for handling user uploads:
public async Task<IActionResult> UploadFile(IFormFile file)
{
var blobServiceClient = new BlobServiceClient(_connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient("user-uploads");
// Generate unique filename to prevent collisions
var fileName = $"{Guid.NewGuid()}-{file.FileName}";
var blobClient = containerClient.GetBlobClient(fileName);
// Set content properties
var options = new BlobUploadOptions
{
HttpHeaders = new BlobHttpHeaders
{
ContentType = file.ContentType
},
Metadata = new Dictionary<string, string>
{
{ "uploaded-by", "user-123" },
{ "original-name", file.FileName }
}
};
using var stream = file.OpenReadStream();
await blobClient.UploadAsync(stream, options);
return Ok(new { url = blobClient.Uri.ToString() });
}
Scenario 2: Data Processing Pipeline
Process uploaded data files automatically:
User uploads CSV → Blob trigger → Azure Function processes → Output to another blob
// Azure Function with Blob Trigger
public async Task Run(
[BlobTrigger("uploads/{name}")] Stream inputBlob,
string name,
[Blob("processed")] BlobContainerClient outputContainer)
{
// Read CSV
using var reader = new StreamReader(inputBlob);
var data = await reader.ReadToEndAsync();
// Process data
var processedData = ProcessCsvData(data);
// Save result
var outputBlob = outputContainer.GetBlobClient($"processed-{name}");
await using var ms = new MemoryStream(Encoding.UTF8.GetBytes(processedData));
await outputBlob.UploadAsync(ms);
}
Scenario 3: Backup & Archive Strategy
Automated backup to different tiers:
┌─────────────────────────────────────────────────────────────┐
│ Upload (Hot) → 30 days (Cool) → 90 days (Archive) │
│ │
│ Use Lifecycle Policy to automatically move blobs │
└─────────────────────────────────────────────────────────────┘
Scenario 4: Image Processing with Azure Functions
Process uploaded images:
public async Task ProcessImage(
[BlobTrigger("images/{name}")] Stream image,
[Blob("thumbnails")] BlobContainerClient thumbnails,
string name)
{
// Resize image
using var input = Image.Load(image);
input.Mutate(x => x.Resize(200, 200));
// Save thumbnail
var thumbName = Path.GetFileNameWithoutExtension(name) + "_thumb.jpg";
var thumbBlob = thumbnails.GetBlobClient(thumbName);
using var ms = new MemoryStream();
input.SaveAsJpeg(ms);
ms.Position = 0;
await thumbBlob.UploadAsync(ms, overwrite: true);
}
Scenario 5: Large File Upload with Chunking
Handle large files by splitting into chunks:
public async Task UploadLargeFile(string filePath, int chunkSize = 4 * 1024 * 1024)
{
var blobClient = _containerClient.GetBlobClient("largefile.dat");
using var fileStream = File.OpenRead(filePath);
var buffer = new byte[chunkSize];
var blockIds = new List<string>();
int blockNumber = 0;
while (fileStream.Read(buffer, 0, buffer.Length) > 0)
{
var blockId = Convert.ToBase64String(BitConverter.GetBytes(blockNumber++));
blockIds.Add(blockId);
using var memStream = new MemoryStream(buffer);
await blobClient.stageBlockAsync(blockId, memStream);
}
await blobClient.CommitBlockListAsync(blockIds);
}
Scenario 6: Generating SAS URLs for Secure Access
Generate time-limited URLs for client access:
public string GenerateSasUrl(string blobName, TimeSpan expiry)
{
var blobClient = _containerClient.GetBlobClient(blobName);
var sasBuilder = new BlobSasBuilder
{
BlobContainerName = _containerName,
BlobName = blobName,
Resource = "b",
ExpiresOn = DateTimeOffset.UtcNow.Add(expiry)
};
sasBuilder.SetPermissions(BlobSasPermissions.Read);
var sasToken = sasBuilder.ToSasQueryParameters(
_storageAccount.GetCredentials(),
_storageAccount.Name);
return $"{blobClient.Uri}?{sasToken}";
}
Common Issues & Solutions
| Issue | Solution |
|---|---|
| File too large | Use chunked upload or Block Blobs |
| Slow upload | Increase concurrency in transfer options |
| Invalid content type | Set Content-Type in BlobHttpHeaders |
| Access denied | Check SAS token permissions or RBAC |
Next Steps
- Explore Public vs Private Access
- Learn about Connection Strings & Keys
- Understand SAS Tokens for secure access
Azure Integration Hub - Beginner Level