LiteX.Storage.Google 4.1.0

This client library enables working with the Google Blob service for storing binary data. Wrapper around the Google Blob to handle container instantiations for any type of application (ASP.NET Core, .Net Standard 2.x). A very simple .NET interface to blob storage and basic wrapper around the Google Storage API.

There is a newer version of this package available.
See the version list below for details.
Install-Package LiteX.Storage.Google -Version 4.1.0
dotnet add package LiteX.Storage.Google --version 4.1.0
<PackageReference Include="LiteX.Storage.Google" Version="4.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteX.Storage.Google --version 4.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

LiteXStorage

Abstract interface to implement any kind of basic blob storage services (e.g. Azure, Amazon, Google, Local FileSystem) for any type of application (ASP.NET Core, .Net Standard 2.x).

Add a dependency

Nuget

Run the nuget command for installing the client as,

  • Install-Package LiteX.Storage.Core
  • Install-Package LiteX.Storage.Google

Configuration

AppSettings

{
  //LiteX Google Storage settings
  "GoogleCloudBlobConfig": {
    "GoogleProjectId": "--- REPLACE WITH YOUR GOOGLE PROJECT ID ---",
    "GoogleJsonAuthPath": "--- REPLACE WITH YOUR GOOGLE JSON AUTH PATH ---",
    "GoogleBucketName": "--- REPLACE WITH YOUR GOOGLE BUCKET NAME ---"
  }
}

Startup Configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // 1. Use default configuration from appsettings.json's 'GoogleCloudBlobConfig'
        services.AddLiteXGoogleCloudBlobService();

        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXGoogleCloudBlobService(option =>
        {
            //option. = "";
        });

        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var googleCloudBlobConfig = new GoogleCloudBlobConfig();
        services.AddLiteXGoogleCloudBlobService(googleCloudBlobConfig);
    }
}

Usage

Controller or Business layer

/// <summary>
/// Customer controller
/// </summary>
[Route("api/[controller]")]
public class CustomerController : Controller
{
    #region Fields

    private readonly IBlobService _blobService;

    #endregion

    #region Ctor

    /// <summary>
    /// Ctor
    /// </summary>
    /// <param name="blobService"></param>
    public CustomerController(IBlobService blobService)
    {
        _blobService = blobService;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Create/Replace blob file
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public IActionResult UploadFile(IFormFile file)
    {
        try
        {
            string blobName = file.FileName;
            Stream stream = file.OpenReadStream();
            string contentType = file.ContentType;
            BlobProperties properties = new BlobProperties { ContentType = contentType };

            bool isUploaded = _blobService.UploadBlob(blobName, stream, properties);
            //bool isUploaded = await _blobService.UploadBlobAsync(blobName, stream, properties);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob file data (bytes or stream)
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobFile(string blobName)
    {
        try
        {
            // get blob
            Stream stream = _blobService.GetBlob(blobName);
            //Stream stream = await _blobService.GetBlobAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob url
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobUrl(string blobName)
    {
        try
        {
            string blobUrl = _blobService.GetBlobUrl(blobName);
            //string blobUrl = await _blobService.GetBlobUrlAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob sas url
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobSasUrl(string blobName)
    {
        try
        {
            string blobUrl = _blobService.GetBlobSasUrl(blobName, DateTimeOffset.UtcNow.AddHours(2), BlobUrlAccess.Read);
            //string blobUrl = await _blobService.GetBlobSasUrlAsync(blobName, DateTimeOffset.UtcNow.AddHours(2), BlobUrlAccess.Read);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Delete blob file
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult DeleteBlobFile(string blobName)
    {
        try
        {
            bool isDeleted = _blobService.DeleteBlob(blobName);
            //bool isDeleted = await _blobService.DeleteBlobAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob metadata
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobMetadata(string blobName)
    {
        try
        {
            BlobDescriptor blobDescriptor = _blobService.GetBlobDescriptor(blobName);
            //BlobDescriptor blobDescriptor =  await _blobService.GetBlobDescriptorAsync(blobName);
            var metadata = blobDescriptor.Metadata;
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Set blob metadata
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult SetBlobMetadata(string blobName)
    {
        try
        {
            IDictionary<string, string> metadata = new Dictionary<string, string>();

            BlobProperties properties = new BlobProperties()
            {
                ContentType = "",
                Metadata = metadata,
                ContentDisposition = "",
                Security = BlobSecurity.Public
            };

            bool isSet = _blobService.SetBlobProperties(blobName, properties);
            //bool isSet = await _blobService.SetBlobPropertiesAsync(blobName, properties);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob list
    /// </summary>
    /// <returns></returns>
    public IActionResult GetBlobs()
    {
        try
        {
            List<BlobDescriptor> blobs = _blobService.GetBlobs().ToList();
            //List<BlobDescriptor> blobs = await _blobService.GetBlobsAsync().ToList();
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    #endregion

    #region Utilities

    private IList<Customer> GetCustomers()
    {
        IList<Customer> customers = new List<Customer>();

        customers.Add(new Customer() { Id = 1, Username = "ashish", Email = "toaashishpatel@outlook.com" });

        return customers;
    }

    private Customer GetCustomerById(int id)
    {
        Customer customer = null;

        customer = GetCustomers().ToList().FirstOrDefault(x => x.Id == id);

        return customer;
    }

    #endregion
}

LiteXStorage

Abstract interface to implement any kind of basic blob storage services (e.g. Azure, Amazon, Google, Local FileSystem) for any type of application (ASP.NET Core, .Net Standard 2.x).

Add a dependency

Nuget

Run the nuget command for installing the client as,

  • Install-Package LiteX.Storage.Core
  • Install-Package LiteX.Storage.Google

Configuration

AppSettings

{
  //LiteX Google Storage settings
  "GoogleCloudBlobConfig": {
    "GoogleProjectId": "--- REPLACE WITH YOUR GOOGLE PROJECT ID ---",
    "GoogleJsonAuthPath": "--- REPLACE WITH YOUR GOOGLE JSON AUTH PATH ---",
    "GoogleBucketName": "--- REPLACE WITH YOUR GOOGLE BUCKET NAME ---"
  }
}

Startup Configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // 1. Use default configuration from appsettings.json's 'GoogleCloudBlobConfig'
        services.AddLiteXGoogleCloudBlobService();

        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXGoogleCloudBlobService(option =>
        {
            //option. = "";
        });

        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var googleCloudBlobConfig = new GoogleCloudBlobConfig();
        services.AddLiteXGoogleCloudBlobService(googleCloudBlobConfig);
    }
}

Usage

Controller or Business layer

/// <summary>
/// Customer controller
/// </summary>
[Route("api/[controller]")]
public class CustomerController : Controller
{
    #region Fields

    private readonly IBlobService _blobService;

    #endregion

    #region Ctor

    /// <summary>
    /// Ctor
    /// </summary>
    /// <param name="blobService"></param>
    public CustomerController(IBlobService blobService)
    {
        _blobService = blobService;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Create/Replace blob file
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public IActionResult UploadFile(IFormFile file)
    {
        try
        {
            string blobName = file.FileName;
            Stream stream = file.OpenReadStream();
            string contentType = file.ContentType;
            BlobProperties properties = new BlobProperties { ContentType = contentType };

            bool isUploaded = _blobService.UploadBlob(blobName, stream, properties);
            //bool isUploaded = await _blobService.UploadBlobAsync(blobName, stream, properties);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob file data (bytes or stream)
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobFile(string blobName)
    {
        try
        {
            // get blob
            Stream stream = _blobService.GetBlob(blobName);
            //Stream stream = await _blobService.GetBlobAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob url
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobUrl(string blobName)
    {
        try
        {
            string blobUrl = _blobService.GetBlobUrl(blobName);
            //string blobUrl = await _blobService.GetBlobUrlAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob sas url
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobSasUrl(string blobName)
    {
        try
        {
            string blobUrl = _blobService.GetBlobSasUrl(blobName, DateTimeOffset.UtcNow.AddHours(2), BlobUrlAccess.Read);
            //string blobUrl = await _blobService.GetBlobSasUrlAsync(blobName, DateTimeOffset.UtcNow.AddHours(2), BlobUrlAccess.Read);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Delete blob file
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult DeleteBlobFile(string blobName)
    {
        try
        {
            bool isDeleted = _blobService.DeleteBlob(blobName);
            //bool isDeleted = await _blobService.DeleteBlobAsync(blobName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob metadata
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult GetBlobMetadata(string blobName)
    {
        try
        {
            BlobDescriptor blobDescriptor = _blobService.GetBlobDescriptor(blobName);
            //BlobDescriptor blobDescriptor =  await _blobService.GetBlobDescriptorAsync(blobName);
            var metadata = blobDescriptor.Metadata;
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Set blob metadata
    /// </summary>
    /// <param name="blobName"></param>
    /// <returns></returns>
    public IActionResult SetBlobMetadata(string blobName)
    {
        try
        {
            IDictionary<string, string> metadata = new Dictionary<string, string>();

            BlobProperties properties = new BlobProperties()
            {
                ContentType = "",
                Metadata = metadata,
                ContentDisposition = "",
                Security = BlobSecurity.Public
            };

            bool isSet = _blobService.SetBlobProperties(blobName, properties);
            //bool isSet = await _blobService.SetBlobPropertiesAsync(blobName, properties);
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    /// <summary>
    /// Get blob list
    /// </summary>
    /// <returns></returns>
    public IActionResult GetBlobs()
    {
        try
        {
            List<BlobDescriptor> blobs = _blobService.GetBlobs().ToList();
            //List<BlobDescriptor> blobs = await _blobService.GetBlobsAsync().ToList();
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
        return Ok();
    }

    #endregion

    #region Utilities

    private IList<Customer> GetCustomers()
    {
        IList<Customer> customers = new List<Customer>();

        customers.Add(new Customer() { Id = 1, Username = "ashish", Email = "toaashishpatel@outlook.com" });

        return customers;
    }

    private Customer GetCustomerById(int id)
    {
        Customer customer = null;

        customer = GetCustomers().ToList().FirstOrDefault(x => x.Id == id);

        return customer;
    }

    #endregion
}

Release Notes

Async support.
     Re-write again from scratch.
     Added new methods support (e.g. create/delete container, SasUrl, GetBlobDescriptor, GetBlobs, Get/Set BlobProperties).
     Core logic changes.
     Use data stream instead of byte array.
     One default container/bucket specific.
     Namespace changed.
     Bug fixes.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
7.0.0 296 8/4/2019
6.4.0 296 1/31/2019
6.3.0 134 11/15/2018
6.2.0 162 10/10/2018
6.1.0 142 8/26/2018
6.0.0 187 7/22/2018
5.0.0 193 6/27/2018
4.1.0 255 6/16/2018
4.0.0 218 6/2/2018
3.0.0 216 5/7/2018
2.0.0 229 4/25/2018
1.0.0 243 4/22/2018
Show less