How to Integrate Cloudinary File Storage Service in .NET Core

Here, we will learn about integrating the Cloudinary API in .NET Core. Cloudinary enables you to programmatically add image and video upload, manipulation, optimization, and delivery capabilities to your applications via easy-to-use REST APIs. Furthermore, Cloudinary offers a variety of SDK libraries that wrap Cloudinary’s REST APIs, enabling you to seamlessly integrate Cloudinary functionality with your existing application code.

 1) Create and Setup your Account

  • If you don’t have a Cloudinary account yet, you can sign up for a free account now.The sign-up form includes an option to select the cloud name for your account. That cloud name will appear in the URL of every asset you deliver from Cloudinary, so you should pick a cloud name that’s a good representation of your organization, application or website. If you don’t choose one, a randomly generated cloud name will be assigned.

2) Get Familiar with Cloudinary Console

  • Account Details

    The account details section on the Dashboard page shows your cloud name identifier, as well as your API key and secret, which you will need in order to configure your SDK or to directly run API requests.

    The Environment variable combines all three of these values and can be copied for quick configuration of your SDK and other server-side integrations.

 

3) Create .NET Core API Project and configure cloudinary settings

  • Configure cloudinary API key & Secrets in appsettings.json
"CloudinarySettings": {
    "Cloudname": "Your Cloud Name",
    "ApiKey": "Your Cloudinary Api Key",
    "ApiSecret": "Your Cloudinary Api Secrets"
  },
  • Create Photoservice.cs
using CloudinaryDemo.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using CloudinaryDotNet.Actions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using CloudinaryDotNet;
using System.Threading.Tasks;
using CloudinaryDemo.Configuration;
using Microsoft.Extensions.Configuration;
using CloudinaryDemo.Entities;

namespace CloudinaryDemo.Services
{
    public class PhotoService : IPhotoService
    {
        private readonly Cloudinary _cloudinary;
        private readonly IConfiguration _cnf;
        private readonly IUnitOfWork _unitOfWork;
        public PhotoService(IConfiguration cnf, IOptions<CloudinarySettings> config, IUnitOfWork unitOfWork)
        {
            _cnf = cnf;
            _unitOfWork = unitOfWork;

            var acc = new Account
            (
                config.Value.CloudName,
                config.Value.ApiKey,
                config.Value.ApiSecret
            );
            _cloudinary = new Cloudinary(acc);
        }
        public async Task<ImageUploadResult> AddItemPhotoAsync(IFormFile file)
        {
            var uploadResult = new ImageUploadResult();
            if (file.Length > 0)
            {
                using var stream = file.OpenReadStream();
                var uploadParams = new ImageUploadParams
                {
                    File = new FileDescription(file.FileName, stream),
                    //Transformation = new Transformation().Crop("fit").Height(200).Width(200) //.Crop("fill").Gravity("face")
                };
                uploadResult = await _cloudinary.UploadAsync(uploadParams);
            }
            return uploadResult;
        }
       
     
    }
}
  • Create Iphotoservice
using System.Threading.Tasks;
using CloudinaryDotNet.Actions;
using Microsoft.AspNetCore.Http;

namespace CloudinaryDemo.Interfaces
{
    public interface IPhotoService
    {
        Task<ImageUploadResult> AddItemPhotoAsync(IFormFile file);
    }
}
  • Registered PhotoService in Startup.cs
  • While Invoking Services you need to simply pass Iformfile image Object in parameter and it will return image storage url.

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories