GithubHelp home page GithubHelp logo

mfkimbell / azure-container-and-blob-management Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 893 KB

A .NET8 application that allows users to add, delete, and view the contents of Blobs and Containers.

C# 56.31% HTML 38.02% CSS 4.78% JavaScript 0.89%

azure-container-and-blob-management's Introduction

Azure Container and Blob Management

Tools Used:

  • Azure blob storage
  • c#
  • .NET 8
  • blobServiceClient

Purpose:

The purpose of this application is to edit, add, and delete containers and blobs on Azure from a .Net application, as well as display images from a specified container.

Add Blob

Screenshot 2023-11-26 at 12 07 45 PM

Delete Blob

deleteBlob

Create Container

Screenshot 2023-11-26 at 12 07 45 PM

Delete Container

Screenshot 2023-11-26 at 12 07 45 PM

Metadata Display

Screenshot 2023-11-26 at 12 07 45 PM

We use blobServiceClient C# package to manage, delete, and add containers.

In the main page, we display the current heiarchy of containers and blobs:

<div class="p-4">
    <h4 class="text-primary"> Blob Storage Heiarchy</h4>

    @foreach(var item in Model)
    {
        <p>@item</p>
    }
</div>

Our “homeController” is responsible for rendering all current Containers and Blobs on startup. We have a for loop in Index.cshtml that renders all of the items. To create that list, in the ContainerService, we have “GetAllContainersAndBlobs” which adds containers then a nested loop that gets the blobs for each container

await foreach (BlobItem blobItem in _blobContainer.GetBlobsAsync())
{
    // get metadata (_blobClient is the blob service client)
    var blobClient = _blobContainer.GetBlobClient(blobItem.Name);
    BlobProperties blobProperties = await blobClient.GetPropertiesAsync();
    string blobToAdd = blobItem.Name;
    if (blobProperties.Metadata.ContainsKey("title"))
    {
        blobToAdd += "(" + blobProperties.Metadata["title"] + ")";
    }
    containerNamesAndBlobNames.Add("------"+blobToAdd);
Screenshot 2023-11-26 at 12 07 45 PM

When "container" is selected,

Screenshot 2023-11-26 at 12 07 45 PM

In index.cshtml, we have buttons that do “asp-actions” and they lead to functions in the "asp-controller" designated controller.

The controller will return a “view” with the data passed in, and our frontend will update.

We create interfaces for blobService and containerService to outline basic commands like “get all blobs” “get one blob” “delete blob” etc… We make “blobService.cs” and “containerService.cs” with functions that will make our controller’s code more clean and simple.

We implement views for each of the controller’s functions to update the screen with new info. Using these functions, CRUD functions, I can alter data on Azure account with code. The functions execute, then the view is refrehsed with the updated data.

Files receive a hash when inputted to prevent overwriting of duplicate names:

 var fileName = Path.GetFileNameWithoutExtension(file.FileName)+"_"+Guid.NewGuid()+Path.GetExtension(file.FileName);

We display the images in the images tab with the public URI, this doesn’t work on privagte containers (obviously), but we can use a shared access signature (SAS) to get a working URI.

string sasContainerSignature = "";

if (blobContainerClient.CanGenerateSasUri)
{
    BlobSasBuilder sasBuilder = new()
    {
        BlobContainerName = blobContainerClient.Name,
        Resource = "c", // c for container
        ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
    };

    sasBuilder.SetPermissions(BlobSasPermissions.Read);

    sasContainerSignature = blobContainerClient.GenerateSasUri(sasBuilder).AbsoluteUri.Split('?')[1].ToString();
}

await foreach (var item in blobs)
{
    var blobClient = blobContainerClient.GetBlobClient(item.Name);
    Blob blobIndividual = new()
    {
        Uri = blobClient.Uri.AbsoluteUri + "?" + sasContainerSignature
    };

To to add comment and title, we specify meta data on the blob object upon creation:

 BlobProperties blobProperties = await blobClient.GetPropertiesAsync();
                if (blobProperties.Metadata.ContainsKey("title"))
                {
                    blobIndividual.Title = blobProperties.Metadata["title"];
                }
                if (blobProperties.Metadata.ContainsKey("comment"))
                {
                    blobIndividual.Comment = blobProperties.Metadata["comment"];
                }
                blobList.Add(blobIndividual);
            }
            return blobList;

And we reuse that metadata when displaying titiles in the file heiarchy and in the images page:

<div class="row">
        @foreach(var item in Model)
        {
            <div class="col-4 border p-2">
                <h4 class="text-success">@item.Title</h4>
                <img src="@item.Uri" width="100%" />
			</div>
        }
    </div>

azure-container-and-blob-management's People

Contributors

mfkimbell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.