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
<PackageReference Include="FGV.Lib.Persistence.Storage.Gcp" Version="1.0.0" />
<PackageVersion Include="FGV.Lib.Persistence.Storage.Gcp" Version="1.0.0" />
<PackageReference Include="FGV.Lib.Persistence.Storage.Gcp" />
paket add FGV.Lib.Persistence.Storage.Gcp --version 1.0.0
#r "nuget: FGV.Lib.Persistence.Storage.Gcp, 1.0.0"
#:package FGV.Lib.Persistence.Storage.Gcp@1.0.0
#addin nuget:?package=FGV.Lib.Persistence.Storage.Gcp&version=1.0.0
#tool nuget:?package=FGV.Lib.Persistence.Storage.Gcp&version=1.0.0
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 filefileName
: 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 contentfileName
: 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 storageMimeType
: 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 | Versions 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. |
-
net7.0
- Google.Cloud.Storage.V1 (>= 4.7.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 6.0.1)
- MimeMapping (>= 2.0.0)
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 |