Client for C# SDK

This commit is contained in:
p.gross 2024-05-30 15:10:49 +03:00
commit 2f2755cbcb
2 changed files with 98 additions and 0 deletions

84
Program.cs Normal file
View file

@ -0,0 +1,84 @@
using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap;
try
{
var fsClient = new Client("KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq", "http://172.29.238.97:8080");
// List containers
Console.WriteLine("Existing containers:");
await foreach(var cid in fsClient.ListContainersAsync())
{
Console.WriteLine(cid.ToString());
// await fsClient.DeleteContainerAsync(cid);
// Console.WriteLine($"REmoved container: {cid}");
}
// Create container
var placementPolicy = new PlacementPolicy(true, new Replica(1));
var containerId = await fsClient.CreateContainerAsync(new Container(BasicAcl.PublicRW, placementPolicy));
Console.WriteLine($"Created container: {containerId}");
Container? container = null;
while(container is null)
{
try
{
container = await fsClient.GetContainerAsync(containerId);
}
catch
{
Console.WriteLine("Waiting for the container is ready");
}
}
var currentDir = Directory.GetCurrentDirectory();
// Put object
using var fileStream = File.OpenRead("cat.jpg");
var cat = new ObjectHeader(
containerId: containerId,
type: ObjectType.Regular,
new ObjectAttribute("Filename", "cat.jpg")
);
var objectId = await fsClient.PutObjectAsync(cat, fileStream);
// Get object header
var objHeader = await fsClient.GetObjectHeadAsync(containerId, objectId);
Console.WriteLine("Attributes:");
foreach(var attribute in objHeader.Attributes)
{
Console.WriteLine($"{attribute.Key}:{attribute.Value}");
}
// Get object back
var obj = await fsClient.GetObjectAsync(containerId, objectId);
File.WriteAllBytes("downloaded.jpg", obj.Payload);
Console.WriteLine("Existing containers after creating:");
await foreach(var cid in fsClient.ListContainersAsync())
{
Console.WriteLine($"ContainerId: {cid} (removing...)");
await fsClient.DeleteContainerAsync(cid);
}
await Task.Delay(2000);
await foreach(var cid in fsClient.ListContainersAsync())
{
Console.WriteLine($"Container {cid} is alive!");
}
}
catch(Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}

14
client.csproj Normal file
View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\FrostFS.SDK.ClientV2\FrostFS.SDK.ClientV2.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>