FGV.Lib.Persistence.Storage.Gcp 1.0.0

dotnet add package FGV.Lib.Persistence.Storage.Gcp --version 1.0.0
                    
NuGet\Install-Package FGV.Lib.Persistence.Storage.Gcp -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FGV.Lib.Persistence.Storage.Gcp" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Persistence.Storage.Gcp" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FGV.Lib.Persistence.Storage.Gcp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FGV.Lib.Persistence.Storage.Gcp --version 1.0.0
                    
#r "nuget: FGV.Lib.Persistence.Storage.Gcp, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FGV.Lib.Persistence.Storage.Gcp@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FGV.Lib.Persistence.Storage.Gcp&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FGV.Lib.Persistence.Storage.Gcp&version=1.0.0
                    
Install as a Cake Tool

FGV.Lib.Persistence.Storage.Gcp

A .NET library that simplifies integration with Google Cloud Storage (GCP) buckets for file operations. This library provides a clean, easy-to-use interface for common storage operations such as uploading, downloading, and deleting files from GCP buckets.

Features

  • Upload files to GCP buckets (from base64 strings or memory streams)
  • Download files from GCP buckets
  • Retrieve file information with proper MIME types
  • Delete files from GCP buckets
  • Dependency injection support
  • Async operation support

Installation

Via NuGet Package Manager

Install-Package FGV.Lib.Persistence.Storage.Gcp

Via .NET CLI

dotnet add package FGV.Lib.Persistence.Storage.Gcp

Configuration

The library requires configuration for GCP authentication and bucket settings. You can provide these through dependency injection in your application startup.

Dependency Injection Setup

using FGV.Lib.Persistence.Storage.Gcp;
using FGV.Lib.Persistence.Storage.Gcp.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

// In your Startup.cs or Program.cs
public void ConfigureServices(IServiceCollection services)
{
    // Register the GCP Storage service
    services.AddScoped<IGcpStorage, GcpStorage>();
    
    // If using appsettings.json for configuration
    services.AddSingleton<IConfiguration>(Configuration);
}

Configuration Settings

Add the following to your appsettings.json:

{
"GcpStorage": {
    "BucketName": "your-bucket-name",
    "AuthKeyJson": "path-to-your-gcp-auth-key.json"
}
}

Alternatively, you can provide the authentication key directly as JSON:

{
"GcpStorage": {
    "BucketName": "your-bucket-name",
    "AuthKeyJson": "{\"type\":\"service_account\",\"project_id\":\"your-project-id\",...}"
}
}

Usage Examples

Uploading a File

// From base64 string
public async Task UploadFileExample(IGcpStorage gcpStorage)
{
    string base64Content = "base64-encoded-file-content";
    string fileName = "example.pdf";
    
    var uploadResult = await gcpStorage.SaveFileAsync(base64Content, fileName);
    string fileUrl = uploadResult.FileUrl;
}

// From memory stream
public async Task UploadFileFromStreamExample(IGcpStorage gcpStorage)
{
    using var stream = new MemoryStream();
    // Add data to the stream
    
    string fileName = "example.jpg";
    var uploadResult = await gcpStorage.SaveFileAsync(stream, fileName);
    string fileUrl = uploadResult.FileUrl;
}

Downloading a File

public async Task DownloadFileExample(IGcpStorage gcpStorage)
{
    string fileName = "example.pdf";
    var downloadResult = await gcpStorage.DownloadFileAsync(fileName);
    
    // Access the file content as a byte array
    byte[] fileContent = downloadResult.FileContent;
    
    // Access the file's MIME type
    string mimeType = downloadResult.MimeType;
}

Getting File Information

public async Task GetFileExample(IGcpStorage gcpStorage)
{
    string fileName = "example.jpg";
    var fileInfo = await gcpStorage.GetFileAsync(fileName);
    
    // Access the file URL
    string fileUrl = fileInfo.FileUrl;
    
    // Access the MIME type
    string mimeType = fileInfo.MimeType;
}

Deleting a File

public async Task DeleteFileExample(IGcpStorage gcpStorage)
{
    string fileName = "example.pdf";
    bool success = await gcpStorage.DeleteFileAsync(fileName);
    
    if (success)
    {
        Console.WriteLine("File successfully deleted");
    }
}

API Documentation

IGcpStorage Interface

SaveFileAsync(string base64, string fileName)

Saves a file to GCP storage from a base64 string.

  • Parameters:
  • base64: The base64-encoded content of the file
  • fileName: The name of the file to be saved
  • Returns: A GcpStorageResponse containing the file URL and MIME type
SaveFileAsync(MemoryStream stream, string fileName)

Saves a file to GCP storage from a memory stream.

  • Parameters:
  • stream: The memory stream containing the file content
  • fileName: The name of the file to be saved
  • Returns: A GcpStorageResponse containing the file URL and MIME type
GetFileAsync(string fileName)

Gets file information from GCP storage.

  • Parameters:
  • fileName: The name of the file to retrieve information for
  • Returns: A GcpStorageResponse containing the file URL and MIME type
DownloadFileAsync(string fileName)

Downloads a file from GCP storage.

  • Parameters:
  • fileName: The name of the file to download
  • Returns: A GcpStorageResponseWithContent containing the file content, URL, and MIME type
DeleteFileAsync(string fileName)

Deletes a file from GCP storage.

  • Parameters:
  • fileName: The name of the file to delete
  • Returns: true if the file was successfully deleted, false otherwise

Response Classes

GcpStorageResponse
  • FileUrl: The public URL of the file in GCP storage
  • MimeType: The MIME type of the file
GcpStorageResponseWithContent (inherits from GcpStorageResponse)
  • FileContent: The content of the file as a byte array

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome. Please feel free to submit a Pull Request.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 499 5/29/2025