0
0
AzureHow-ToBeginner · 3 min read

How to Upload File to Azure Blob Storage Quickly

To upload a file to Azure Blob Storage, use the BlobServiceClient to connect to your storage account, then get a BlobContainerClient and a BlobClient for your target blob. Finally, call uploadFile() on the BlobClient with your file path to upload the file.
📐

Syntax

Uploading a file to Azure Blob Storage involves these steps:

  • BlobServiceClient: Connects to your storage account using a connection string.
  • BlobContainerClient: Represents the container (folder) where blobs are stored.
  • BlobClient: Represents the specific blob (file) to upload.
  • uploadFile(filePath): Uploads the local file at filePath to the blob.
javascript
const { BlobServiceClient } = require('@azure/storage-blob');

async function upload() {
  const blobServiceClient = BlobServiceClient.fromConnectionString('<connection-string>');
  const containerClient = blobServiceClient.getContainerClient('<container-name>');
  const blobClient = containerClient.getBlobClient('<blob-name>');

  await blobClient.uploadFile('<local-file-path>');
}

upload().catch(console.error);
💻

Example

This example shows how to upload a local file named example.txt to a container called mycontainer in Azure Blob Storage using Node.js.

javascript
import { BlobServiceClient } from '@azure/storage-blob';
import path from 'path';

async function uploadFile() {
  const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
  if (!connectionString) {
    throw new Error('Azure Storage connection string not found in environment variables');
  }

  const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
  const containerName = 'mycontainer';
  const containerClient = blobServiceClient.getContainerClient(containerName);

  // Ensure container exists
  await containerClient.createIfNotExists();

  const localFilePath = path.resolve('./example.txt');
  const blobName = 'uploaded-example.txt';
  const blobClient = containerClient.getBlobClient(blobName);

  await blobClient.uploadFile(localFilePath);
  console.log(`File uploaded to blob storage as: ${blobName}`);
}

uploadFile().catch(console.error);
Output
File uploaded to blob storage as: uploaded-example.txt
⚠️

Common Pitfalls

Common mistakes when uploading files to Azure Blob Storage include:

  • Using an incorrect or missing connection string causes authentication failures.
  • Not creating the container before uploading results in errors.
  • Incorrect blob or container names with invalid characters cause failures.
  • Trying to upload a file path that does not exist throws file not found errors.

Always check your environment variables, container existence, and file paths before uploading.

javascript
/* Wrong: Missing container creation */
const containerClient = blobServiceClient.getContainerClient('mycontainer');
// uploadFile will fail if container does not exist
await containerClient.getBlobClient('file.txt').uploadFile('file.txt');

/* Right: Create container if missing */
await containerClient.createIfNotExists();
await containerClient.getBlobClient('file.txt').uploadFile('file.txt');
📊

Quick Reference

Remember these key points when uploading files:

  • Use BlobServiceClient.fromConnectionString() to connect.
  • Get or create your container with getContainerClient() and createIfNotExists().
  • Use getBlobClient() to specify the file name in storage.
  • Call uploadFile() with the local file path to upload.

Key Takeaways

Always connect to Azure Blob Storage using a valid connection string.
Create the container if it does not exist before uploading files.
Use BlobClient's uploadFile method with the correct local file path.
Check for correct container and blob names to avoid errors.
Handle errors gracefully to know if upload succeeds or fails.