Saturday, 28 May 2022

Push to aws S3 bucket through AWS CLI

 Setup AWS CLI

1. Download AWS CLI msi file from aws 

 

Get Access Key and Secret access key 

1. Login to AWS console

2. go to IAM , users, click on the user u want secret key for

3. select user and click "Create Access Key"

4. Download csv file and save it somewhere safe


Pushing to S3 Bucket

1. aws s3 cp . s3://nageshswatchlist/ --recursive

use --recursive option to upload all files in directory


Resolving CORS issue using reverse proxy

1. install heroku cli 

2. git clone https://github.com/Rob--W/cors-anywhere.git

https://github.com/Rob--W/cors-anywhere

3. npm install

4. heroku create

Creating app... !
 !    Invalid credentials provided.
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/53511e72-3354-44e0-a45e-80e2f5fe5510?requestor=SFMyNTY.g2gDbQAAAA8xMDYuMjEwLjE0Ny4yMTRuBgDIHn0KgQFiAAFRgA.W0AyFZigs1NJHJxkkq0UPACmR12Lc_46TszBnb9i32A
Logging in... done
Logged in as ajab.nagesh@gmail.com
Creating app... done, ⬢ salty-oasis-62166
https://salty-oasis-62166.herokuapp.com/ | https://git.heroku.com/salty-oasis-62166.git

5. git push heroku master

This will create a reverse proxy server

6. Now while making ajax call just put this app url before ur api endpoint 

corsurl+ baseUrl + 'action/insertOne',

 


Friday, 20 May 2022

Index

Secure a Web Api in ASP.NET Core

Secure a Web Api in ASP.NET Core - The Blinking Caret

You can download source code from here

https://github.com/nageshajab/SecureNetCore6Api.git

 

 

Using mongodb with .net core

 1. Add nuget package MongoDB.Driver

2. Add a Book class to the Models directory with the following code:

C#
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace BookStoreApi.Models;

public class Book
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string? Id { get; set; }

    [BsonElement("Name")]
    public string BookName { get; set; } = null!;

    public decimal Price { get; set; }

    public string Category { get; set; } = null!;

    public string Author { get; set; } = null!;
}3. add connection string "BookStoreDatabase": {
        "ConnectionString": "mongodb://localhost:27017",
        "DatabaseName": "BookStore",
        "BooksCollectionName": "Books"
    }4. Add a CRUD operations service
using BookStoreApi.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BookStoreApi.Services;

public class BooksService
{
private readonly IMongoCollection<Book> _booksCollection;

public BooksService(
IOptions<BookStoreDatabaseSettings> bookStoreDatabaseSettings)
{
var mongoClient = new MongoClient(
bookStoreDatabaseSettings.Value.ConnectionString);

var mongoDatabase = mongoClient.GetDatabase(
bookStoreDatabaseSettings.Value.DatabaseName);

_booksCollection = mongoDatabase.GetCollection<Book>(
bookStoreDatabaseSettings.Value.BooksCollectionName);
}

public async Task<List<Book>> GetAsync() =>
await _booksCollection.Find(_ => true).ToListAsync();

public async Task<Book?> GetAsync(string id) =>
await _booksCollection.Find(x => x.Id == id).FirstOrDefaultAsync();

public async Task CreateAsync(Book newBook) =>
await _booksCollection.InsertOneAsync(newBook);

public async Task UpdateAsync(string id, Book updatedBook) =>
await _booksCollection.ReplaceOneAsync(x => x.Id == id, updatedBook);

public async Task RemoveAsync(string id) =>
await _booksCollection.DeleteOneAsync(x => x.Id == id);
}
 
  

 

Thursday, 19 May 2022

Using dependency Injection in .Net Core 6

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using static System.Guid;

namespace myWatchList
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //setup our DI
            var serviceProvider = new ServiceCollection()
                .AddLogging(c => c.AddConsole(opt => opt.LogToStandardErrorThreshold = LogLevel.Debug))
                .AddSingleton<IFooService, FooService>()
                .AddSingleton<IBarService, BarService>()
                .BuildServiceProvider();
                        
            //configure console logging
            serviceProvider
                .GetService<ILoggerFactory>();
          
            var logger = serviceProvider.GetService<ILoggerFactory>()
                .CreateLogger<Program>();
            logger.LogInformation("Starting application");

            //do the actual work here
            var bar = serviceProvider.GetService<IBarService>();
            bar.DoSomeRealWork();

            logger.LogDebug("All done!");
        }
    }

    public interface IFooService
    {
        void DoThing(int number);
    }

    public interface IBarService
    {
        void DoSomeRealWork();
    }
    public class BarService : IBarService
    {
        private readonly IFooService _fooService;
        public BarService(IFooService fooService)
        {
            _fooService = fooService;
        }

        public void DoSomeRealWork()
        {
            for (int i = 0; i < 10; i++)
            {
                _fooService.DoThing(i);
            }
        }
    }

    public class FooService : IFooService
    {
        private readonly ILogger<FooService> _logger;
        public FooService(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<FooService>();
        }

        public void DoThing(int number)
        {
            _logger.LogInformation($"Doing the thing {number}");
        }
    }

}

Search This Blog

Creating your first "Alexa" Skill

Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...