using System.Collections.ObjectModel; using FrostFS.SDK; using FrostFS.SDK.Client; using Grpc.Core; using Grpc.Net.Client; using Microsoft.Extensions.Options; class Demo { private const string Key = "KzPXA6669m2pf18XmUdoR8MnP1pi1PMmefiFujStVFnv7WR5SRmK"; private const string Host = "http://172.23.32.4:8080"; static void Main(string[] args) { } #region Network public static async Task GetNetmapSnapshotAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); return await client.GetNetmapSnapshotAsync(default); } public static async Task GetNodeInfoAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); return await client.GetNodeInfoAsync(default); } public static async Task GetNetworkSettingsAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); return await client.GetNetworkSettingsAsync(default); } #endregion #region Session public static async Task GetSessionTokenAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); return await client.CreateSessionAsync(new PrmSessionCreate(int.MaxValue), default); } #endregion #region Chain public static async Task> AddChainAsync(string targetString, string name, byte[] raw) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var target = new FrostFsChainTarget(Enum.Parse(targetString), name); var chain = new FrostFsChain(raw); var args = new PrmApeChainAdd(target, chain); return await client.AddChainAsync(args, default); } public static async Task RemoveChainAsync(string targetString, string name, byte[] raw) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var target = new FrostFsChainTarget(Enum.Parse(targetString), name); var chain = new FrostFsChain(raw); var args = new PrmApeChainRemove(target, chain); await client.RemoveChainAsync(args, default); } public static async Task RemoveChainAsync(string targetString, string name) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var target = new FrostFsChainTarget(Enum.Parse(targetString), name); var args = new PrmApeChainList(target); await client.ListChainAsync(args, default); } #endregion #region Container public static async Task CreateContainerAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var createContainerParam = new PrmContainerCreate( new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))), PrmWait.DefaultParams); return await client.CreateContainerAsync(createContainerParam, default); } public static async Task GetContainerAsync() { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var createContainerParam = new PrmContainerCreate( new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))), PrmWait.DefaultParams); return await client.CreateContainerAsync(createContainerParam, default); } public static async Task DeleteContainerAsync(FrostFsContainerId containerId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmContainerDelete(containerId, new PrmWait(60, 5)); await client.DeleteContainerAsync(args, default); } public static async Task> ListContainersAsync(FrostFsContainerId containerId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmContainerGetAll(); Collection containers = []; await foreach (var cid in client.ListContainersAsync(args, default)) { containers.Add(cid); } return containers; } #endregion #region Object public static async Task GetObjectHeadAsync(FrostFsContainerId containerId, FrostFsObjectId objectId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmObjectHeadGet(containerId, objectId); return await client.GetObjectHeadAsync(args, default); } public static async Task GetObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmObjectGet(containerId, objectId); return await client.GetObjectAsync(args, default); } public static async Task GetRangeAsync(FrostFsContainerId containerId, FrostFsObjectId objectId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmRangeGet(containerId, objectId, new FrostFsRange(64, 128)); var rangeReader = await client.GetRangeAsync(args, default); var downloadedBytes = new byte[128]; MemoryStream ms = new(downloadedBytes); ReadOnlyMemory? chunk; while ((chunk = await rangeReader!.ReadChunk()) != null) { ms.Write(chunk.Value.Span); } return downloadedBytes; } public static async Task[]> GetRangeHashAsync(FrostFsContainerId containerId, FrostFsObjectId objectId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); Random rnd = new(); var salt = new byte[32]; rnd.NextBytes(salt); var args = new PrmRangeHashGet(containerId, objectId, [new FrostFsRange(100, 64)], salt); return await client.GetRangeHashAsync(args, default); } public static async Task PutObjectAsync(FrostFsContainerId containerId, byte[] bytes) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmObjectPut( new FrostFsObjectHeader( containerId: containerId, type: FrostFsObjectType.Regular, [new FrostFsAttributePair("fileName", "test")])); var stream = await client.PutObjectAsync(args, default); await stream.WriteAsync(bytes.AsMemory()); var objectId = await stream.CompleteAsync(); return objectId; } public static async Task PutSingleObjectAsync(FrostFsObject obj) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmSingleObjectPut(obj); return await client.PutSingleObjectAsync(args, default); } public static async Task PatchObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId, byte[] patch) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var range = new FrostFsRange(64, (ulong)patch.Length); var args = new PrmObjectPatch( new FrostFsAddress(containerId, objectId), payload: new MemoryStream(patch), maxChunkLength: 256, range: range); return await client.PatchObjectAsync(args, default); } public static async Task DeleteObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId, byte[] patch) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var args = new PrmObjectDelete(containerId, objectId); await client.DeleteObjectAsync(args, default); } public static async Task> SearchObjectsAsync(FrostFsContainerId containerId) { var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host }); Func grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url)); var client = FrostFSClient.GetInstance(clientOptions, grpcChannel); var filter = new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test"); var args = new PrmObjectSearch(containerId, null, [], filter); Collection objects = []; await foreach (var objId in client.SearchObjectsAsync(args, default)) { objects.Add(objId); } return objects; } #endregion }