DEV Community

1suleyman
1suleyman

Posted on

Project 2: Azure Storage for File Management

Introduction

In this project, I focused on Azure Blob Storage, where I learned how to set up a storage account, create containers, and upload or retrieve files programmatically. Additionally, I explored Azure Storage Explorer for easy file management.

Project Overview

This project helped me:

  • Set up an Azure Storage Account.
  • Create a Blob Storage container.
  • Upload files using Azure Storage Explorer.
  • Optionally, I built a simple C# app to upload and retrieve files programmatically.

Let me walk you through the steps I followed to complete this project.

Step 1: Set Up an Azure Storage Account

1️⃣ Create a Storage Account:

I opened the Azure Portal and clicked on Create a resource at the top left.

In the Search the Marketplace bar, I searched for Storage account and selected it from the results.
Then, I clicked Create.

2️⃣ Filled in Storage Account Details:

  • Subscription: I selected the correct subscription I was working under.
  • Resource Group: I created a new resource group called az-rg1 (for organizational purposes).
  • Storage Account Name: I gave my storage account a unique name like az104bobstg1.
  • Region: I chose UK South since it was the closest to me.
  • Performance: I selected Standard for the performance tier.
  • Replication: I went with Locally Redundant Storage (LRS) for simplicity.

3️⃣ Review and Create: After filling in all the details, I clicked Review + Create and then Create. I waited for the deployment to complete.

4️⃣ Access Your Storage Account: Once the storage account was created, I navigated to the Resource Group and selected my new storage account.

Step 2: Set Up Blob Storage Containers

1️⃣ Navigate to Blob Service:
In the Storage Account, I went to the Blob Service section and clicked on Containers.

2️⃣ Create a New Blob Container:

  1. I clicked + Container.
  2. I provided a name for my container (e.g., my-container).
  3. I set the Public access level to Private for secure file storage.
  4. I clicked Create to set up the container.

Step 3: Upload Files Using Azure Storage Explorer

1️⃣ Install Azure Storage Explorer:
I downloaded and installed Azure Storage Explorer from here.

2️⃣ Connect Azure Storage Explorer to My Azure Account:
I opened Azure Storage Explorer.
Clicked on the plug icon (Account Management) and chose Connect to Azure Storage.
I selected Azure Account and signed in with my Azure credentials.

3️⃣ Navigate to My Storage Account:
In the left panel, I expanded Azure → Storage Accounts.
I found my storage account by navigating through Subscriptions → Resource Groups → My Storage Account.
I expanded the account and clicked on Blob Containers.

4️⃣ Upload Files to Blob Container:
I right-clicked on the Blob Container I created earlier.
Selected Upload → Upload Files.
I selected the file(s) from my local machine and clicked Open.
The files were uploaded and appeared in the container once the upload was complete.

Step 4: Build a Simple App to Upload and Retrieve Files Programmatically
Now, I decided to build a simple C# Console App to interact with my Blob Storage.

1️⃣ Install .NET SDK (if not installed already):
I downloaded and installed the .NET SDK from here.

2️⃣ Create a Console App in Visual Studio Code:
I opened Visual Studio Code and created a new directory for my project:
mkdir MyConsoleApp
cd MyConsoleApp
code .

Created a new console app using:
dotnet new console
This generated a new Program.cs file.

3️⃣ Install the Azure.Storage.Blobs NuGet Package:
I opened the terminal in Visual Studio Code and ran the following command to install the Azure.Storage.Blobs package:
dotnet add package Azure.Storage.Blobs

4️⃣ Write Code to Upload Files: I replaced the default code with the following to upload files to Azure Blob Storage:
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
static async Task Main(string[] args)
{
// Connection string for your Azure Storage account
string connectionString = "";

    // The container name in Azure Blob Storage
    string containerName = "my-container"; 

    // Local file path to upload
    string filePath = @"C:\path\to\your\file.txt";

    // The name the file will have in Blob Storage
    string blobName = "file.txt";

    // Initialize BlobServiceClient and BlobContainerClient
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    // Upload file to blob storage
    Console.WriteLine($"Uploading file to {blobName}...");
    await blobClient.UploadAsync(filePath, overwrite: true);
    Console.WriteLine("Upload completed.");
}
Enter fullscreen mode Exit fullscreen mode

}

You can find the code for uploading files in my GitHub - Code to Upload Files.

Step 5: Retrieve Files from Azure Blob Storage

1️⃣ Write Code to Retrieve Files: To retrieve a file from Blob Storage, I used the following code:
using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
static async Task Main(string[] args)
{
string connectionString = "";
string containerName = "my-container";
string blobName = "file.txt"; // The name of the file to download
string downloadFolderPath = @"C:\path\to\download";

    string downloadFilePath = Path.Combine(downloadFolderPath, blobName);

    try
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        Console.WriteLine($"Downloading {blobName}...");
        await blobClient.DownloadToAsync(downloadFilePath);

        Console.WriteLine($"Download completed!\nThe file is saved as {downloadFilePath}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"An error occurred: {ex.Message}");
    }
}
Enter fullscreen mode Exit fullscreen mode

}

You can find the code for retrieving files in my GitHub - Code to Retrieve Files.

Step 6: Conclusion and Cleanup
1️⃣ Test Your Application: I ensured the app was working correctly by uploading and downloading files from my Azure Blob Storage container.

2️⃣ Verify Upload and Download: I went to Azure Storage Explorer or the Azure Portal to confirm that the files were uploaded successfully and could be retrieved.

3️⃣ Clean Up Resources: After I was done, I deleted the storage account and all associated resources from the Azure portal to avoid unnecessary charges.

Conclusion

In this project, I successfully set up Azure Blob Storage, created containers, and uploaded and retrieved files using Azure Storage Explorer. Additionally, I built a C# Console App to interact with Blob Storage programmatically, allowing me to upload and download files easily.

By the end of this project, I gained hands-on experience with Azure Blob Storage, Azure Storage Explorer, and programmatic file management using C# in Azure. I can now implement these tools to manage files within Azure and integrate them into applications efficiently.

Credits: https://www.datacamp.com/blog/azure-project-ideas?dc_referrer=https%3A%2F%2Fwww.google.com%2F

You can find the full code for this project on my GitHub - Code to Upload Files and GitHub - Code to Retrieve Files.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay