[#4] sample for custom Client Cut

Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
Pavel Gross 2024-06-10 11:11:09 +03:00
parent 79764c9b67
commit edf10b970b

View file

@ -26,7 +26,7 @@ using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums; using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap; using FrostFS.SDK.ModelsV2.Netmap;
var fsClient = new Client(<your_key>, <your_host>); var fsClient = Client.GetInstance(<your_key>, <your_host>);
// List containers // List containers
var containersIds = await fsClient.ListContainersAsync(); var containersIds = await fsClient.ListContainersAsync();
@ -55,7 +55,7 @@ using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums; using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap; using FrostFS.SDK.ModelsV2.Netmap;
var fsClient = new Client(<your_key>, <your_host>); var fsClient = Client.GetInstance(<your_key>, <your_host>);
// Search regular objects // Search regular objects
var objectsIds = await fsClient.SearchObjectAsync( var objectsIds = await fsClient.SearchObjectAsync(
@ -78,3 +78,76 @@ var objHeader = await fsClient.GetObjectHeadAsync(cId, oId);
// Get object // Get object
var obj = await fsClient.GetObjectAsync(cId, oId); 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(<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, 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);
}
```