220 lines
No EOL
6.7 KiB
C#
220 lines
No EOL
6.7 KiB
C#
using System.Diagnostics;
|
|
|
|
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;
|
|
|
|
try
|
|
{
|
|
var fsClient = Client.GetInstance("KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq", "http://172.29.238.97:8080");
|
|
|
|
await FetchAndRemoveContainers(fsClient);
|
|
|
|
ContainerId containerId = await CreateContainer(fsClient);
|
|
|
|
ShowFileInExplorer();
|
|
|
|
var fileName = "cat.jpg";
|
|
//var fileName = "fat_cat.bmp";
|
|
|
|
var objectId = await PutObject(fsClient, containerId, fileName);
|
|
//var objectId = await PutSingleObject(fsClient, containerId, fileName);
|
|
//var objectId = await PutObjectClientCut(fsClient, containerId, fileName);
|
|
|
|
var filter = new ObjectFilter(ObjectMatchType.Equals, "fileName", fileName);
|
|
await foreach (var objId in fsClient.SearchObjectsAsync(containerId, filter))
|
|
{
|
|
await GetObjectInfo(fsClient, containerId, objId);
|
|
}
|
|
|
|
var @object = await fsClient.GetObjectAsync(containerId, objectId!);
|
|
|
|
File.WriteAllBytes(GetBroFileName(fileName), @object.Payload);
|
|
|
|
await foreach (var cid in fsClient.ListContainersAsync())
|
|
{
|
|
Console.WriteLine($"ContainerId: {cid} (removing...)");
|
|
await fsClient.DeleteContainerAsync(cid);
|
|
}
|
|
|
|
await Task.Delay(500);
|
|
|
|
await foreach (var cid in fsClient.ListContainersAsync())
|
|
{
|
|
Console.WriteLine($"Container {cid} still alive!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|
}
|
|
|
|
static async Task FetchAndRemoveContainers(IFrostFSClient fsClient)
|
|
{
|
|
Console.WriteLine("Existing containers:");
|
|
|
|
await foreach (var cid in fsClient.ListContainersAsync())
|
|
{
|
|
await fsClient.DeleteContainerAsync(cid);
|
|
Console.WriteLine($"Removed container: {cid}");
|
|
}
|
|
}
|
|
|
|
static async Task<ContainerId> CreateContainer(IFrostFSClient fsClient)
|
|
{
|
|
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
|
|
{
|
|
await Task.Delay(1000);
|
|
container = await fsClient.GetContainerAsync(containerId);
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("Waiting for the container is ready. Some time is required");
|
|
}
|
|
}
|
|
|
|
return containerId;
|
|
}
|
|
|
|
static async Task<ObjectId> PutObject(IFrostFSClient fsClient, ContainerId containerId, string fileName)
|
|
{
|
|
var fileStream = File.OpenRead(fileName);
|
|
|
|
var header = new ObjectHeader(
|
|
containerId: containerId,
|
|
type: ObjectType.Regular,
|
|
new ObjectAttribute("fileName", fileName)
|
|
);
|
|
|
|
return await fsClient.PutObjectAsync(header, fileStream);
|
|
}
|
|
|
|
static async Task<ObjectId> PutSingleObject(IFrostFSClient fsClient, ContainerId containerId, string fileName)
|
|
{
|
|
var bytes = File.ReadAllBytes(fileName);
|
|
|
|
var obj = new FrostFS.SDK.ModelsV2.Object(containerId, bytes)
|
|
.AddAttribute("Filename", fileName);
|
|
|
|
var objectId = await fsClient.PutSingleObjectAsync(obj);
|
|
|
|
return objectId;
|
|
}
|
|
|
|
static async Task GetObjectInfo(IFrostFSClient fsClient, ContainerId containerId, ObjectId objectId)
|
|
{
|
|
Console.WriteLine("--------------------------------------------------");
|
|
Console.WriteLine($"Object ID: {objectId}");
|
|
|
|
var objHeader = await fsClient.GetObjectHeadAsync(containerId, objectId);
|
|
|
|
Console.WriteLine($"size: {objHeader.PayloadLength}");
|
|
|
|
if (objHeader.Split?.SplitId != null)
|
|
Console.WriteLine($"splitID: { objHeader.Split?.SplitId}");
|
|
|
|
if (objHeader.Split?.Parent != null)
|
|
Console.WriteLine($"parent: {objHeader.Split?.Parent}");
|
|
|
|
if (objHeader.Split?.Previous != null)
|
|
Console.WriteLine($"previous: {objHeader.Split?.Previous}");
|
|
|
|
Console.WriteLine($"Attributes:");
|
|
foreach (var attribute in objHeader.Attributes)
|
|
{
|
|
Console.WriteLine($"\t{attribute.Key} = {attribute.Value}");
|
|
}
|
|
|
|
if (objHeader.Split?.Children?.FirstOrDefault() != null)
|
|
{
|
|
Console.WriteLine($"Children:");
|
|
objHeader.Split?.Children.ForEach(x => Console.WriteLine(x.Value));
|
|
}
|
|
}
|
|
|
|
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)
|
|
.AddAttribute("type", $"part_{sentObjectIds.Count}")
|
|
.SetSplit(split);
|
|
|
|
if (largeObject.PayloadLength == fullLength)
|
|
break;
|
|
|
|
var objectId = await fsClient.PutSingleObjectAsync(currentObject);
|
|
sentObjectIds.Add(objectId);
|
|
}
|
|
|
|
if (sentObjectIds.Any())
|
|
{
|
|
largeObject.CalculateHash()
|
|
.AddAttribute("type", $"ParentObject")
|
|
.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)
|
|
.AddAttribute("type", $"LinkedObject");
|
|
|
|
_ = await fsClient.PutSingleObjectAsync(linkObject);
|
|
|
|
//return fsClient.CalculateObjectId(largeObject.Header);
|
|
|
|
return currentObject.GetParentId();
|
|
}
|
|
|
|
return await fsClient.PutSingleObjectAsync(currentObject);
|
|
}
|
|
|
|
static void ShowFileInExplorer()
|
|
{
|
|
Directory.SetCurrentDirectory(@"C:\Users\p.gross\Pictures\samples");
|
|
var currentDir = Directory.GetCurrentDirectory();
|
|
Process.Start("explorer.exe", currentDir);
|
|
}
|
|
|
|
static string GetBroFileName(string path)
|
|
{
|
|
var newName = Path.GetFileNameWithoutExtension(path) + "_bro" + Path.GetExtension(path);
|
|
return newName;
|
|
} |