R2.NET
1.0.2
dotnet add package R2.NET --version 1.0.2
NuGet\Install-Package R2.NET -Version 1.0.2
<PackageReference Include="R2.NET" Version="1.0.2" />
paket add R2.NET --version 1.0.2
#r "nuget: R2.NET, 1.0.2"
// Install R2.NET as a Cake Addin #addin nuget:?package=R2.NET&version=1.0.2 // Install R2.NET as a Cake Tool #tool nuget:?package=R2.NET&version=1.0.2
R2.NET
R2.NET is a .NET client for interacting with Cloudflare R2. This package provides a simple and efficient way to manage your Cloudflare R2 buckets and objects. It includes both direct client initiation and a factory pattern for generating and fetching clients.
Installation
To install R2.NET, run the following command in your .NET project:
dotnet add package R2.NET
Configuration
Add the following configuration to your appsettings.json:
{
"CloudflareR2": {
"ApiBaseUri": "https://api.cloudflare.com/client/v4/accounts",
"AccountId": "your-account-id",
"ApiToken": "your-api-token"
}
}
Usage
Example 1: Direct Client Initiation
This example demonstrates how to directly initiate the Cloudflare R2 client and use it to upload, retrieve, and delete blobs.
using System;
using System.IO;
using System.Net.Http;
using Microsoft.Extensions.Options;
namespace YourNamespace
{
public class ClientUsage
{
public async Task RunAsync()
{
var options = Options.Create(new CloudflareR2Options
{
ApiBaseUri = "https://api.cloudflare.com/client/v4/accounts",
AccountId = "your-account-id",
ApiToken = "your-api-token"
});
var httpClient = new HttpClient();
var client = new CloudflareR2Client(httpClient, options);
// Upload Blob
using var fileStream = File.OpenRead("path/to/your/file.zip");
var blobUrl = await client.UploadBlobAsync("your-bucket-name", "your-blob-name.zip", fileStream, "application/zip");
// Get Blob
using var blobStream = await client.GetBlobAsync("your-bucket-name", "your-blob-name.zip");
// Delete Blob
await client.DeleteBlobAsync("your-bucket-name", "your-blob-name.zip");
}
}
}
Example 2: Best Practice Using the Factory
This example demonstrates the best practice of using the factory pattern to generate and fetch clients.
Step 1: Register the Services in DI
Add the following to your Startup.cs
or Program.cs
:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CloudflareR2Options>(Configuration.GetSection(CloudflareR2Options.SettingsName));
services.AddSingleton<ICloudflareR2ClientFactory, CloudflareR2ClientFactory>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
}
}
Step 2: Use the factory in your service to create or fetch clients:
public class MyService
{
private readonly ICloudflareR2ClientFactory _clientFactory;
public MyService(ICloudflareR2ClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task UploadFileAsync(string clientName, string bucketName, string objectName, Stream fileStream)
{
var client = _clientFactory.GetClient(clientName);
await client.UploadBlobAsync(bucketName, objectName, fileStream, "application/octet-stream");
}
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- AWSSDK.Core (>= 3.7.400)
- AWSSDK.Extensions.NETCore.Setup (>= 3.7.301)
- AWSSDK.S3 (>= 3.7.400)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.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.2 | 43 | 7/29/2024 |