WIP: C# implementation of FrostFS SDK
Find a file
Pavel Gross 22e2a53551 [#16] Client: Unit tests
Signed-off-by: Pavel Gross <p.gross@yando.com>
2024-08-16 12:09:17 +03:00
.forgejo/workflows Add empty repo 2024-04-24 14:59:21 +03:00
src [#16] Client: Unit tests 2024-08-16 12:09:17 +03:00
.gitignore [#11] Add Network Snapshot 2024-06-26 12:29:33 +03:00
FrostFS.SDK.sln [#11] Add Network Snapshot 2024-06-26 12:29:33 +03:00
LICENSE Initial commit 2024-04-24 09:46:03 +00:00
README.md [#7] Client cut internal 2024-06-14 12:07:15 +00:00

frostfs-sdk-csharp

C# implementation of FrostFS SDK.

Prerequisites

Get the key for your wallet

  1. Get the address
cat <path_to_your_wallet> | jq .accounts[0].address | tr -d '"'
  1. Get the key
neo-go wallet export -w <path_to_your_wallet> -d <address_from_p1>

Example usage

Container

using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap;

var fsClient = Client.GetInstance(<your_key>, <your_host>);

// List containers
var containersIds = await fsClient.ListContainersAsync();

// Create container
var placementPolicy = new PlacementPolicy(true, new Replica(1));
var containerId = await fsClient.CreateContainerAsync(
    new Container(
        BasicAcl.PublicRW, 
        placementPolicy
    )
);

// Get container
var container = await fsClient.GetContainerAsync(cId);

// Delete container
await fsClient.DeleteContainerAsync(containerId);

Object

using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap;

var fsClient = Client.GetInstance(<your_key>, <your_host>);

// Search regular objects
var objectsIds = await fsClient.SearchObjectAsync(
    cId, 
    ObjectFilter.RootFilter()
);

// Put object
var f = File.OpenRead("cat.jpg");
var cat = new ObjectHeader(
    containerId: cId,
    type: ObjectType.Regular,
    new ObjectAttribute("Filename", "cat.jpg")
);
var oId = await fsClient.PutObjectAsync(cat, f);

// Get object header
var objHeader = await fsClient.GetObjectHeadAsync(cId, oId);

// Get object
var obj = await fsClient.GetObjectAsync(cId, oId);

Custom client cut


using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap;
using FrostFS.SDK.ClientV2.Extensions;
using FrostFS.SDK.ClientV2.Interfaces;

var fsClient = Client.GetInstance(<your_key>, <your_host>);

ContainerId containerId = <containerId>;
string fileName = <fileName>;

await PutObjectClientCut(fsClient, containerId, fileName);

static async Task<ObjectId?> PutObjectClientCut(IFrostFSClient fsClient, ContainerId containerId, string fileName)
{
    List<ObjectId> sentObjectIds = [];
    FrostFS.SDK.ModelsV2.Object? currentObject;

    var partSize = 1024 * 1024;
    var buffer = new byte[partSize];

    var largeObject = new LargeObject(containerId);
    
    var split = new Split();
   
    var fileInfo = new FileInfo(fileName);
    var fullLength = (ulong)fileInfo.Length;
    var fileNameAttribute = new ObjectAttribute("fileName", fileInfo.Name);

    using var stream = File.OpenRead(fileName);
    while (true)
    {
        var bytesCount = await stream.ReadAsync(buffer.AsMemory(0, partSize));

        split.Previous = sentObjectIds.LastOrDefault();

        largeObject.AppendBlock(buffer, bytesCount);
     
        currentObject = new FrostFS.SDK.ModelsV2.Object(containerId, bytesCount < partSize ? buffer.Take(bytesCount).ToArray() : buffer)
            .AddAttribute(fileNameAttribute)
            .SetSplit(split);
   
        if (largeObject.PayloadLength == fullLength)
            break;

        var objectId = await fsClient.PutSingleObjectAsync(currentObject);
        sentObjectIds.Add(objectId);
    }

    if (sentObjectIds.Any())
    {
        largeObject.CalculateHash()
            .AddAttribute(fileNameAttribute);

        currentObject.SetParent(largeObject);
      
        var objectId = await fsClient.PutSingleObjectAsync(currentObject);
        sentObjectIds.Add(objectId);

        var linkObject = new LinkObject(containerId, split.SplitId, largeObject)
            .AddChildren(sentObjectIds)
            .AddAttribute(fileNameAttribute);

        _ = await fsClient.PutSingleObjectAsync(linkObject);

        return currentObject.GetParentId();
    }

     return await fsClient.PutSingleObjectAsync(currentObject);
}