From edf10b970be7fc65257d987537386bc962ee397f Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Mon, 10 Jun 2024 11:11:09 +0300 Subject: [PATCH] [#4] sample for custom Client Cut Signed-off-by: Pavel Gross --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 832b42f..542a544 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ using FrostFS.SDK.ModelsV2; using FrostFS.SDK.ModelsV2.Enums; using FrostFS.SDK.ModelsV2.Netmap; -var fsClient = new Client(, ); +var fsClient = Client.GetInstance(, ); // List containers var containersIds = await fsClient.ListContainersAsync(); @@ -55,7 +55,7 @@ using FrostFS.SDK.ModelsV2; using FrostFS.SDK.ModelsV2.Enums; using FrostFS.SDK.ModelsV2.Netmap; -var fsClient = new Client(, ); +var fsClient = Client.GetInstance(, ); // Search regular objects var objectsIds = await fsClient.SearchObjectAsync( @@ -77,4 +77,77 @@ var objHeader = await fsClient.GetObjectHeadAsync(cId, oId); // Get object var obj = await fsClient.GetObjectAsync(cId, oId); +``` + +### Custom client cut +```csharp + +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(, ); + +ContainerId containerId = ; +string fileName = ; + +await PutObjectClientCut(fsClient, containerId, fileName); + +static async Task PutObjectClientCut(IFrostFSClient fsClient, ContainerId containerId, string fileName) +{ + List 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, buffer) + .AddAttribute(fileNameAttribute) + .SetSplit(split); + + if (largeObject.PayloadLength == fullLength) + break; + + var objectId = await fsClient.PutSingleObjectAsync(currentObject); + sentObjectIds.Add(objectId); + } + + if (sentObjectIds.Any()) + { + largeObject.CalculateHash(); + + var linkObject = new LinkObject(containerId, split.SplitId, largeObject) + .AddChildren(sentObjectIds); + + _ = await fsClient.PutSingleObjectAsync(linkObject); + + currentObject.SetParent(largeObject); + _ = await fsClient.PutSingleObjectAsync(currentObject); + + return currentObject.GetParentId(); + } + + return await fsClient.PutSingleObjectAsync(currentObject); +} + ``` \ No newline at end of file