Wednesday 27 December 2023

AWS - Create Copy of DynamoDB Table and learn Backup and Restore Functionality

AWS DynamoDB Back and Restore


There is no direct way to copy DynamoDB tables as of now. So, we must take a backup and restore the table with a new name. Let's learn.


Step 1: Search DynamoDB from the search bar and Click Tabes on the left side. Select the table from the right that you want to take the backup.



















Step 2: Click on the Backup Tab, click the Create Back dropdown below, and select Create an on-demand backup.
















Step 3: Create on-demand backup, click the Create Backup button.





















Step 4: You can see the request is in progress













Step 5: You can also see the status in AWS Backup. (Search AWS backup in the Search bar).


















It will take approx 5 minutes to take the backup and it will change the status to complete.
Step 6: After completion, you can Go to Dynamo DB from the search bar and you can find the backup there then you can press the restore button.

















Step 7: Here you can update the table name that you want and press the restore button.































You can see the request submitted successfully.









Step 8: To check the status, you again can go to AWS backup --> Jobs --> and click Restore Jobs.
















Step 9: After completion, you can see a new table Under DynamoDb tables













This way you can create a table copy in the DynamoDB.
That's all about this article, thank you.


Sunday 10 December 2023

Create resource on Azure with Terraform

What is Terraform?

Terraform is an Infrastructure as Code (IaC) software tool offered by HashiCorp. You can provision and manage your infrastructure both on-premises and in the cloud with it. 


How to execute Terraform Scripts?


Prerequisite

1. You need an Azure Portal account
2. Install Azure CLI on your local system. Download the installer from the below link
    Azure CLI Installer
3. Azure Login on command prompt run (az login) command 
4. Install Terraform. Download the installer from the below link. Download 
AMD64 for     64-bit system. Terraform Download

 

Let's create a small script to create a resource Group in the Azure portal.


Step 1: Create a Terraform file to create a resource group. Paste the below script into the new file and the name should be anyname.tf

provider "azurerm" {
  version = "~>2.0"
  features {}
}

resource "azurerm_resource_group" "terraform1" {
  name = "terraform1"
  location = "eastus"
}

Place the file in any drive. I have placed the file in the D drive. Below is my file. My Resource group name is terraform1. You can write any name.











Step 2: Intialize Terraform.


My file is on D drive. So I have executed the command from D drive.
            terraform init










Step 3: Terraform Plan

            Execute command    terraform plan













Step 4: Terraform Apply

            Execute command    terraform apply












It will prompt you to perform this action? Write Yes and Enter.








After yes, it will start creating the infrastructure on the Azure Portal










You can see the success message above. Go to the Azure portal, and you can see a new resource group with the name terraform1.
















That's all about this article. 


Thursday 20 July 2023

Deploy your website to an Azure VM or Local Machine IIS.

The first step is to ensure IIS is installed on the VM. Open the browser and type localhost to ensure IIS is running. Or type inetmgr in Run. If it is not working this means IIS is not installed. 

Install IIS

1. Open the control panel
2. Go To Programs And Features
3. Click on Turn Windows Features on or off
4. Click on Internet Information Services (IIS)
5. Press Ok


















It will install IIS on your system. Now when you open localhost you can see the below screen.

















6. Go to run and type inetmgr. you can view IIS as having a default website.













7. You can publish your code to Default Web Site or create a new Website.

8. Let's publish your code on the default website. Right-click on the default website and click Explore. Publish your code and paste it here.

9. Open the browser and type localhost. You can see your website is running. If you are getting below error

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.


This means you need to install the .Net Runtime on your VM. My API is in .Net 7. So, you can download .net 7 from the link below or from Google based on the specific version.

.Net runtime

10. Hurrah, now you can see your website is running.

This is all about this article. Thank you.

Saturday 1 April 2023

Generate C# .NET Classes of entire database through SQL Scripts Automatically

Let's learn how to generate C# Classes from SQL Scripts. Generate C# classes automatically for the entire database.


Step 1: Copy the query below and run it in the SQL Editor (New Query).
Step 2: Pass the table name as a parameter and run the query.

You can find the entire class in the output below.

DECLARE @TableName sysname = '' 
DECLARE @objectId int=1

      WHILE EXISTS (select 1 from sys.objects where type='U' and 
					name!='__RefactorLog' and object_id > @objectId)
      BEGIN          
        SELECT @objectId = MIN(object_id) FROM sys.objects 
					WHERE TYPE='U' and NAME!='__RefactorLog' 
					AND object_id > @objectId    
		
		SET @TableName=(SELECT NAME FROM sys.objects WHERE object_id=@objectId)

DECLARE @Output VARCHAR(MAX) = 'public class ' + @TableName + ' {' SELECT @Output = @Output + ' public ' + ColumnType + NullSign + ' ' + ColumnName + ' { get; set; }' FROM ( SELECT replace(col.name, ' ', '_') ColumnName, column_id ColumnId, CASE typ.NAME WHEN 'bigint' THEN 'long' WHEN 'binary' THEN 'byte[]' WHEN 'bit' THEN 'bool' WHEN 'char' THEN 'string' WHEN 'date' THEN 'DateTime' WHEN 'datetime' THEN 'DateTime' WHEN 'datetime2' THEN 'DateTime' WHEN 'datetimeoffset' THEN 'DateTimeOffset' WHEN 'decimal' THEN 'decimal' WHEN 'float' THEN 'double' WHEN 'image' THEN 'byte[]' WHEN 'int' THEN 'int' WHEN 'money' THEN 'decimal' WHEN 'nchar' THEN 'string' WHEN 'ntext' THEN 'string' WHEN 'numeric' THEN 'decimal' WHEN 'nvarchar' THEN 'string' WHEN 'real' THEN 'float' WHEN 'smalldatetime' THEN 'DateTime' WHEN 'smallint' THEN 'short' WHEN 'smallmoney' THEN 'decimal' WHEN 'text' THEN 'string' WHEN 'time' THEN 'TimeSpan' WHEN 'timestamp' THEN 'long' WHEN 'tinyint' THEN 'byte' WHEN 'uniqueidentifier' THEN 'Guid' WHEN 'varbinary' THEN 'byte[]' WHEN 'varchar' THEN 'string' ELSE 'UNKNOWN_' + typ.name END ColumnType, CASE WHEN col.is_nullable = 1 and typ.name IN ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset','decimal','float','int','money','numeric','real', 'smalldatetime','smallint','smallmoney','time','tinyint','uniqueidentifier') THEN '?' ELSE '' END NullSign FROM sys.columns col INNER JOIN sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id WHERE object_id = object_id(@TableName) ) temp order by ColumnId set @Output = @Output + ' }' print @Output
END
Here is the output





If you want to generate the classes of a specific table then follow the below article.


I hope you like this article.

Get Images/Files from Azure Storage Blob/Container .NET C#

There are many ways to get files from Azure Storage in .Net and C#. We will discuss 3 different ways today. Before that let's create a storage container on Azure Portal.


1. Create a Storage account in Azure Portal
2. Click on the container, create a new container, and select the public access level.











3. Click on the "Access keys" option and copy the connection string. We will use it in our code.














Let's learn different ways to get the files to Azure Storage. For that, you need to install Azure.Storage.Blobs package from Nuget and you can run the below code based on your requirements.


1. Get bytes/base64 string to Azure Storage

    
[HttpGet(Name = "GetImageBytes")]
public async Task<List<BlobImageModel>> GetImageBytes() { var connectionString = "pass your connection string here"; var container = new BlobContainerClient(connectionString, "containernamehere"); var files = new List<BlobImageModel>(); await foreach (var file in container.GetBlobsAsync()) { string uri = container.Uri.ToString(); var name = file.Name; var fullUri = $"{uri}/{name}"; var blobClient = container.GetBlobClient(file.Name); if (blobClient.ExistsAsync().Result) { using (var ms = new MemoryStream()) { blobClient.DownloadTo(ms); var bytes = ms.ToArray(); files.Add(new BlobImageModel { Name = name, ImageBytes = bytes }); } } } return files; }

public class BlobImageModel { public string? Name { get; set; } public byte[]? ImageBytes { get; set; } }

2. Get Images with URLs from Azure Storage

      
[HttpGet(Name = "GetImagesURL")] public async Task<List<BlobResponseModel>> GetImagesURL() { var connectionString = "connection string here"; var container = new BlobContainerClient(connectionString, "containernamehere"); var files = new List<BlobResponseModel>(); await foreach (var file in container.GetBlobsAsync()) { string uri = container.Uri.ToString(); var name = file.Name; var fullUri = $"{uri}/{name}"; var blobClient = container.GetBlobClient(file.Name); if (blobClient.ExistsAsync().Result) { files.Add(new BlobResponseModel { Uri = fullUri, Name = name, ContentType = file.Properties.ContentType }); } } return files; }

     public class BlobResponseModel
    {
        public string? Uri { get; set; }
        public string? Name { get; set; }
        public string? ContentType { get; set; }
     }

If you want to upload the image to azure storage, follow the article below.

Upload File to Azure Storage


This is all about the Get image from Azure Storage. 

 

Friday 24 March 2023

Read PDF with Azure AI - Form Recognizer in .NET C#

There are multiple ways to read the PDF, In this article, we will discuss Azure AI Form Recognizer. Let's learn how to read PDF in .NET/C# code with Form Recognizer? First of all, let's discuss Form Recognizer.

Form Recognizer is a cloud-based service by Microsoft Azure that allows developers to extract information from Forms and Documents. It includes formats like PDF, Images, and Scanned Documents. Form Recognizer can be accessed through Rest API or SDKs.Net, Python, and Javascript.

Let's first create a form Recognizer in Azure Portal.

Step 1: Go to Azure Portal and create a form recognizer










Step 2: Go to Keys and Endpoint and copy Key and Endpoint and we will use both in the code.


















Step 3: Create your application either console framework or core. You need to add Azure.AI.FormRecognizer Nuget Package in your project. Copy the below code and use it.


In the below sample, I am passing the static Azure Blob PDF path, if you need dynamic PDF, follow the previous article to upload the file in a storage blob and use that URL.



       public async Task<string> Index(IFormFile file)
        {
            var pdfURL = "https://myblog.blob.core.windows.net/sample/Constellation.pdf";
            var uri = new Uri(pdfURL);

            var key = "yourkeyhere";
            var endPoint = "https://yoururl.cognitiveservices.azure.com/";

            AzureKeyCredential credential = new(key);
            var client = new DocumentAnalysisClient(new Uri(endPoint), credential);
            var operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, 
            "prebuilt-document", uri);
            var result = operation.Value;

            // Read key Value Pair
            foreach (DocumentKeyValuePair kvp in result.KeyValuePairs)
            {
                if (kvp.Value == null)
                {
                    Console.WriteLine($"  Found key with no value: '{kvp.Key.Content}'");
                }
                else
                {
                    Console.WriteLine($"Found key-value pair:'{kvp.Key.Content}' and " +
                    $"'{kvp.Value.Content}'");
                }
            }
             // Read the tables here
            if (result.Tables.Count > 0)
            {
                for (int i = 0; i < result.Tables.Count; i++)
                {
                    DocumentTable table = result.Tables[i];
                    Console.WriteLine($"  Table {i} has {table.RowCount} rows " +
                        $"and {table.ColumnCount} columns.");

                    foreach (DocumentTableCell cell in table.Cells)
                    {
                        Console.WriteLine($"Cell ({cell.RowIndex}, {cell.ColumnIndex}) " +
                        $"has kind '{cell.Kind}' and content: '{cell.Content}'.");
                    }
                }
            }

            return string.Empty;
        }

You can see the output in a console window and it will read any PDF you like, This Form Recognzier will cost you around $1.5 for 1000 pages. For detailed pricing, you can follow the Microsoft link below

Form Recognizer Pricing


If you want to read the PDF and image with Azure Cognitive Service, you can follow the below link


Read PDF with Azure Cognitive Service




Upload file to Azure Storage Blob/Container in ASP. Net / C# Code

There are many ways to upload files to Azure Storage. We will discuss 3 different ways today. Before that let's create a storage container on Azure Portal.


1. Create a Storage account in Azure Portal
2. Click on the container and create a new container and select the public access level to the container.











3. Click on the "Access keys" option and copy the connection string. We will use it in our code.














Let's learn different ways to upload files to Azure Storage. For that, you need to install Azure.Storage.Blobs package from Nuget and you can run the below code based on your requirements.


1. Upload bytes/base64 string to Azure Storage


      [HttpPost]
public async Task<string> UploadFile(string imageBase64, string fileName) { if (!string.IsNullOrEmpty(imageBase64)) { var bytes = Convert.FromBase64String(imageBase64); var stream = new MemoryStream(bytes); var connectionString = "Default";// pass connectionstring here var blobServiceClient = new BlobServiceClient(connectionString); //replace containername with your container name
                var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; }
return string.Empty; }

2. Upload IFormFile to Azure Storage


      
[HttpPost]
public async Task<string> UploadFile(IFormFile file,string fileName) { if (file != null) { var stream = file.OpenReadStream(); var connectionString = "Default";// pass connectionstring here var blobServiceClient = new BlobServiceClient(connectionString); var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; } return string.Empty; }


3. Upload Local file to Azure Storage

      
[HttpPost] public async Task<string> UploadFile(string fileName) { string localfilePath = "Pass local file path here"; var stream = System.IO.File.OpenRead(localfilePath); var connectionString = "Default";// pass connectsting here var blobServiceClient = new BlobServiceClient(connectionString); var containerClient = blobServiceClient.GetBlobContainerClient("containername"); if (containerClient == null) { return string.Empty; } var blobClient = containerClient.GetBlobClient(fileName); var result = await blobClient.UploadAsync(stream); return blobClient.Uri.AbsoluteUri; }


This is all about this article.

Monday 20 March 2023

Read PDF text with Azure AI (Azure Cognitive Service)

Azure Cognitive Service makes developer life easy for AI tasks. It provides you with one API Endpoint and with that endpoint, it enables many use cases. 


You can do multiple things with a single cognitive Service like Speech, Text, and Vision use cases.
 
Speech to Text
Transcribe audible speech into readable, searchable text.
Text to Speech
Convert text to lifelike speech for more natural interfaces.
Speech Translation
Integrate real-time speech translation into your apps.

Today, we will create a Vision POC and read PDF/Image files.


Step 1: Create an Azure Cognitive Service in Azure Portal.



Step 2: Click on the Keys and EndPoint option. It will provide you one endpoint and key.











Step 3: Create a new MVC Project and paste the code below in .cshtml file




<form method="post" enctype="multipart/form-data">
    <div class="row">
        <div class="col-8">
            <input type="file" name="file" class="form-control" />
        </div>
        <div class="col-4">
            <button type="submit" class="btn btn-primary">Upload File</button>
        </div>
    </div>
    @ViewBag.extractText
</form>


Step 4: Install Computer Vision Nuget Package









Step 5: Paste the below code in Controller.cs


   public class HomeController : Controller
    {
        private Microsoft.AspNetCore.Hosting.IHostingEnvironment _environment;
    
        public HomeController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
        {
            _environment = hostingEnvironment;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> Index(IFormFile file)
        {
            if (file == null)
            {
                ModelState.Clear();
                ModelState.AddModelError("file", "Please select file first.");
                return View("Index");
            }

            var key = "pass your key here"; 
            var endPoint = "https://write your end point here";

            ComputerVisionClient client = Authenticate(endPoint, key);

            if (file.Length > 0)
            {
                var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "UploadedFiles"));
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (var fileStream = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }

            }

            var contentPath = _environment.ContentRootPath + "\\UploadedFiles\\";
            var fileName = contentPath + file.FileName;
            var text = await ReadImage(client, fileName);
            if (string.IsNullOrEmpty(text))
                text = "No text found.";
            ViewBag.extractText = text;
            FileInfo fileInfo = new FileInfo(fileName);
            if (fileInfo.Exists)//check file exsit or not  
            {
                fileInfo.Delete();
            }

            return View("Index");
        }

     
       public ComputerVisionClient Authenticate(string endpoint, string key)
        {
            ComputerVisionClient client =
              new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
              { Endpoint = endpoint };
            return client;
        }

        public async Task<string> ReadImage(ComputerVisionClient client, string localFile)
        {
            StringBuilder sb = new StringBuilder();

            // Read text from URL
            var textHeaders = await client.ReadInStreamAsync(System.IO.File.OpenRead(localFile));
            // After the request, get the operation location (operation ID)
            string operationLocation = textHeaders.OperationLocation;
            Thread.Sleep(2000);


            const int numberOfCharsInOperationId = 36;
            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            // Extract the text
            ReadOperationResult results;

            do
            {
                results = await client.GetReadResultAsync(Guid.Parse(operationId));
            }
            while ((results.Status == OperationStatusCodes.Running ||
                results.Status == OperationStatusCodes.NotStarted));

            var textUrlFileResults = results.AnalyzeResult.ReadResults;
            foreach (ReadResult page in textUrlFileResults)
            {
                foreach (Line line in page.Lines)
                {
                    sb.AppendLine(line.Text);

                }
            }
            return sb.ToString();
        }
    }


Step 6: Run the project and upload a PDF file






you can see the output below the button, You can extract Specific text based on your need with custom logic.

If you want to read the specific text, then go for the Form Recognizer option in the Azure AI.


To know more about Cognitive Service, you can follow the below link

Azure Cognitive Service



Monday 6 March 2023

Generate C#/.NET Class atomically from SQL Script

Let's learn how to generate C# Classes from SQL Scripts. Generate C# classes automatically.


Step 1: Copy the query below and run it in the SQL Editor (New Query).
Step 2: Pass the table name as a parameter and run the query.

You can find the entire class in the output below.

DECLARE @TableName sysname = 'Person' -- Pass Table Name here

DECLARE @Output VARCHAR(MAX) = 'public class ' + @TableName + ' {' SELECT @Output = @Output + ' public ' + ColumnType + NullSign + ' ' + ColumnName + ' { get; set; }' FROM ( SELECT replace(col.name, ' ', '_') ColumnName, column_id ColumnId, CASE typ.NAME WHEN 'bigint' THEN 'long' WHEN 'binary' THEN 'byte[]' WHEN 'bit' THEN 'bool' WHEN 'char' THEN 'string' WHEN 'date' THEN 'DateTime' WHEN 'datetime' THEN 'DateTime' WHEN 'datetime2' THEN 'DateTime' WHEN 'datetimeoffset' THEN 'DateTimeOffset' WHEN 'decimal' THEN 'decimal' WHEN 'float' THEN 'double' WHEN 'image' THEN 'byte[]' WHEN 'int' THEN 'int' WHEN 'money' THEN 'decimal' WHEN 'nchar' THEN 'string' WHEN 'ntext' THEN 'string' WHEN 'numeric' THEN 'decimal' WHEN 'nvarchar' THEN 'string' WHEN 'real' THEN 'float' WHEN 'smalldatetime' THEN 'DateTime' WHEN 'smallint' THEN 'short' WHEN 'smallmoney' THEN 'decimal' WHEN 'text' THEN 'string' WHEN 'time' THEN 'TimeSpan' WHEN 'timestamp' THEN 'long' WHEN 'tinyint' THEN 'byte' WHEN 'uniqueidentifier' THEN 'Guid' WHEN 'varbinary' THEN 'byte[]' WHEN 'varchar' THEN 'string' ELSE 'UNKNOWN_' + typ.name END ColumnType, CASE WHEN col.is_nullable = 1 and typ.name IN ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') THEN '?' ELSE '' END NullSign FROM sys.columns col INNER JOIN sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id WHERE object_id = object_id(@TableName) ) temp order by ColumnId set @Output = @Output + ' }' print @Output
Here is the output

















If you want to generate the entire database classes in a single go then follow the below article

Generate C# classes of the entire database


I hope you like this article.


How to find the reason of HTTP Error 500.30 - ASP.NET Core app failed to start in Azure App Service

HTTP Error 500.30 - The ASP.NET Core app failed to start If your web app is throwing an error HTTP error 500.30 then how to find the root ca...