Azure

Azure File Share Storage Implementation Utilizing .NET Core 6 Net API

On this article, we’re going to focus on the introduction of azure file share and step-by-step implementation utilizing .NET Core 6 Net API.

Agenda

  • Introduction
  • Advantages
  • Azure File Share Configuration
  • Step-by-step implementation of File share storage utilizing .NET Core 6 Net API

Stipulations

  • Visual Studio 2022
  • .NET Core 6
  • Azure Account

Introduction

  • Azure file share is used to deploy and mounted concurrently by cloud or on-premises deployments.
  • Azure file share gives storage on the cloud utilizing normal SMB protocol.

  • The Server Message Block (SMB) is the community file share protocol that helps us to learn and write information.
  • File share could be accessed from Home windows, Linux, and macOS.
  • NFS File share is accessible from Linux or macOS.

Advantages

  • Straightforward to configure and handle
  • Shared entry between a number of VMs and Server Machine
  • Straightforward to make use of and managed by Azure CLI and Cmdlets

Azure File Share Configuration

Step 1

Open the Azure portal

Step 2

Search storage account and create the identical.

Step 3

Open the storage account and click on on “File Shares” and create a brand new file share.

Azure File Share Storage implementation using .NET Core 6 Web API

Azure File Share Storage implementation using .NET Core 6 Web API

Step 4

Right here we are able to see the file share which we created earlier and all of the information which we’re going to add we are able to discover over right here.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 5

Additionally, open the entry keys part within the storage account and duplicate and save the connection string which we use inside our internet software.

Azure File Share Storage implementation using .NET Core 6 Web API

Step-by-step implementation of File share storage utilizing .NET Core 6 Net API

Step 1

Create a brand new Net API mission.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 2

Configure a brand new mission.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 3

Present further info.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 4

Set up the next NuGet Packages.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 5

Challenge Construction.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 6

Create a File Particulars class.

namespace AzureFileShareDemo
{
    public class FileDetails
    {
        public IFormFile FileDetail { get; set; }
    }
}

Step 7

Subsequent, create IFileShare and FileShare inside repositories.

IFileShare.cs

namespace AzureFileShareDemo.Repositories
{
    public interface IFileShare
    {
        Activity FileUploadAsync(FileDetails fileDetails);
        Activity FileDownloadAsync(string fileShareName);
    }
}

FileShare.cs

utilizing Azure;
utilizing Azure.Storage.Recordsdata.Shares;
utilizing Azure.Storage.Recordsdata.Shares.Fashions;

namespace AzureFileShareDemo.Repositories
{
    public class FileShare : IFileShare
    {
        personal readonly IConfiguration _config;

        public FileShare(IConfiguration config)
        {
            _config = config;
        }

        public async Activity FileUploadAsync(FileDetails fileDetails)
        {
            // Get the configurations and create share object
            ShareClient share = new ShareClient(_config.GetConnectionString("default"), _config.GetValue<string>("FileShareDetails:FileShareName"));

            // Create the share if it would not exist already
            await share.CreateIfNotExistsAsync();

            // Verify the file share is current or not
            if (await share.ExistsAsync())
            {
                // Get a reference to the pattern listing
                ShareDirectoryClient listing = share.GetDirectoryClient("FileShareDemoFiles");

                // Create the listing if it would not exist already
                await listing.CreateIfNotExistsAsync();

                // Be sure that the listing exists
                if (await listing.ExistsAsync())
                {
                    // Get a reference to a file and add it
                    ShareFileClient file = listing.GetFileClient(fileDetails.FileDetail.FileName);

                    // Verify path
                    var filesPath = Listing.GetCurrentDirectory() + "/information";
                    var fileName = Path.GetFileName(fileDetails.FileDetail.FileName);
                    var filePath = Path.Mix(filesPath, fileName);

                    utilizing (FileStream stream = File.OpenRead(filePath))
                    {
                        file.Create(stream.Size);
                        file.UploadRange(
                            new HttpRange(0, stream.Size),
                            stream);
                    }
                }
            }
            else
            {
                Console.WriteLine($"File just isn't add efficiently");
            }
        }

        public async Activity FileDownloadAsync(string fileShareName)
        {
            ShareClient share = new ShareClient(_config.GetConnectionString("default"), _config.GetValue<string>("FileShareDetails:FileShareName"));
            ShareDirectoryClient listing = share.GetDirectoryClient("FileShareDemoFiles");
            ShareFileClient file = listing.GetFileClient(fileShareName);

            // Verify path
            var filesPath = Listing.GetCurrentDirectory() + "/information";
            if (!System.IO.Listing.Exists(filesPath))
            {
                Listing.CreateDirectory(filesPath);
            }

            var fileName = Path.GetFileName(fileShareName);
            var filePath = Path.Mix(filesPath, fileName);

            // Obtain the file
            ShareFileDownloadInfo obtain = file.Obtain();
            utilizing (FileStream stream = File.OpenWrite(filePath))
            {
                await obtain.Content material.CopyToAsync(stream);
            }
        }
    }
}

Step 8

Add the file share title and connection string contained in the appsettings.json file.

{
  "Logging": {
    "LogLevel": {
      "Default": "Info",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "FileShareDetails": {
    "FileShareName": "jdlearningfileshare"
  },
  "ConnectionStrings": {
    "default": ""
  }
}

Step 9

Register a service contained in the Program class.

utilizing AzureFileShareDemo.Repositories;
utilizing FileShare = AzureFileShareDemo.Repositories.FileShare;
var builder = WebApplication.CreateBuilder(args);
// Add companies to the container.
builder.Providers.AddScoped < IFileShare, FileShare > ();
builder.Providers.AddControllers();
// Be taught extra about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Providers.AddEndpointsApiExplorer();
builder.Providers.AddSwaggerGen();
var app = builder.Construct();
// Configure the HTTP request pipeline.
if (app.Atmosphere.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Step 10

Subsequent, create a brand new File Controller.

utilizing AzureFileShareDemo.Repositories;
utilizing Microsoft.AspNetCore.Mvc;

namespace AzureFileShareDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FilesController : ControllerBase
    {
        personal readonly IFileShare fileShare;

        public FilesController(IFileShare fileShare)
        {
            this.fileShare = fileShare;
        }

        /// <abstract>
        /// add file
        /// </abstract>
        /// <param title="fileDetail"></param>
        /// <returns></returns>
        [HttpPost("Upload")]
        public async Activity<IActionResult> UploadFile([FromForm] FileDetails fileDetail)
        {
            if (fileDetail.FileDetail != null)
            {
                await fileShare.FileUploadAsync(fileDetail);
            }
            return Okay();
        }

        /// <abstract>
        /// obtain file
        /// </abstract>
        /// <param title="fileDetail"></param>
        /// <returns></returns>
        [HttpPost("Download")]
        public async Activity<IActionResult> DownloadFile(string fileName)
        {
            if (fileName != null)
            {
                await fileShare.FileDownloadAsync(fileName);
            }
            return Okay();
        }
    }
}

Step 11

Lastly, run the mission and use API endpoints utilizing Swagger UI.

Azure File Share Storage implementation using .NET Core 6 Web API

Step 11

Add a couple of information utilizing Swagger UI

Azure File Share Storage implementation using .NET Core 6 Web API

Step 12

Subsequent, open the azure file share, and inside that, you will discover the information that we uploaded.

Azure File Share Storage implementation using .NET Core 6 Web API

That is all about an azure file share.

GitHub Hyperlink

https://github.com/Jaydeep-007/AzureFileShareDemo

Conclusion

Right here we mentioned the fundamentals of azure file share and the file add and obtain performance through the use of .NET Core Net API.

Completely satisfied Studying!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button