A client for interacting with a Sia Skynet webportal.
This library targets .NET Standard 2.0, so can be used across various platforms. The webportal client is a typed client, that uses System.Net.Http.HttpClient
to make HTTP requests.
using var httpClient = new HttpClient { BaseAddress = new Uri("https://siasky.net") };
var skynetWebPortal = new SkynetWebPortal(httpClient);
When using Microsoft.Extensions.DependencyInjection
, the client can easily be configured and set up to work with any Skynet webportal.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<ISkynetWebPortal, SkynetWebPortal>(client =>
{
client.BaseAddress = new Uri("https://siasky.net");
});
}
Files can be downloaded from a Sia Skynet webportal, by providing a Skylink and optionally a path.
try
{
var skylink = Skylink.Parse("AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA");
HttpContent response = await skynetWebPortal.DownloadFile(skylink);
}
catch(HttpException e)
{
// unsuccessful (non-2XX) response
}
This library uses Microsoft.Extensions.FileProviders.Abstractions
(see FileProviders) which allows you to upload from many file providers. There are several methods available to simplify uploading single files, multiple files and directories.
try
{
var file = new PhysicalFileInfo(new FileInfo("path/to/file.json"));
Skylink response = await skynetWebPortal.UploadFile(file);
}
catch(HttpException e)
{
// unsuccessful (non-2XX) response
}
catch(HttpResponseException e)
{
// invalid response from webportal
}
catch(IOException e)
{
// file access errors
}
try
{
var fileProvider = new PhysicalFileProvider("");
Skylink response = await skynetWebPortal.UploadDirectory(fileProvider, "directory/to/upload", recurse: true);
}
catch(DirectoryNotFoundException e)
{
// invalid directory
}
catch(HttpException e)
{
// unsuccessful (non-2XX) response
}
catch(HttpResponseException e)
{
// invalid response from webportal
}
catch(IOException e)
{
// file access errors
}
By default, the Skynet path of an uploaded file is set to the file name. This behaviour can be changed, by specifying the Skynet path on an individual UploadItem
.
new UploadItem(file, "/images/sunset.jpg");
// file will become available at https://siasky.net/{skylink}/images/sunset.jpg
For file uploads, the MIME type is automatically resolved based on the file extension. If you want to override this behaviour, you can explicitly specify the MIME type on an individual UploadItem
.
new UploadItem(file, null, MediaTypeHeaderValue.Parse("image/gif"));
// when downloaded, the Content-Type header will be set to image/gif
UploadOptions
and MultiFileUploadOptions
can be used to configure how Skynet webportals handle requests for the upload.
var options = new MultiFileUploadOptions
{
FileName = "tag:siasky.net,2020-10-10:AABFphGLnADQbFx3tXOQdtjKf0MvFzqZoDIqj_VaebkqcA",
DefaultPath = "wwwroot/index.html"
DryRun = true
};
Skylink response = await skynetWebPortal.UploadDirectory(fileProvider, "directory/to/upload", recurse: true, options);