Compare commits

...

2 commits

Author SHA1 Message Date
b69d22966f [#7] Client cut internal 2024-06-14 12:07:15 +00:00
545e647d7b [#4] infrastructure and sample Client Cut
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-06-10 11:31:36 +03:00
26 changed files with 896 additions and 192 deletions

View file

@ -1,4 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
#
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion =
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrostFS.SDK.ClientV2", "src\FrostFS.SDK.ClientV2\FrostFS.SDK.ClientV2.csproj", "{50D8F61F-C302-4AC9-8D8A-AB0B8C0988C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrostFS.SDK.Cryptography", "src\FrostFS.SDK.Cryptography\FrostFS.SDK.Cryptography.csproj", "{3D804F4A-B0B2-47A5-B006-BE447BE64B50}"
@ -29,9 +32,8 @@ Global
{5012EF96-9C9E-4E77-BC78-B4111EE54107}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5012EF96-9C9E-4E77-BC78-B4111EE54107}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5012EF96-9C9E-4E77-BC78-B4111EE54107}.Release|Any CPU.Build.0 = Release|Any CPU
{B738F3E1-654D-41A3-B068-58ED122BB688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B738F3E1-654D-41A3-B068-58ED122BB688}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B738F3E1-654D-41A3-B068-58ED122BB688}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B738F3E1-654D-41A3-B068-58ED122BB688}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View file

@ -26,7 +26,7 @@ using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ModelsV2.Enums;
using FrostFS.SDK.ModelsV2.Netmap;
var fsClient = new Client(<your_key>, <your_host>);
var fsClient = Client.GetInstance(<your_key>, <your_host>);
// 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(<your_key>, <your_host>);
var fsClient = Client.GetInstance(<your_key>, <your_host>);
// Search regular objects
var objectsIds = await fsClient.SearchObjectAsync(
@ -77,4 +77,81 @@ 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(<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);
}
```

View file

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using FrostFS.Container;
using FrostFS.Netmap;
using FrostFS.Object;
@ -9,6 +12,7 @@ using FrostFS.SDK.ModelsV2;
using FrostFS.Session;
using Grpc.Core;
using Grpc.Net.Client;
using static FrostFS.Netmap.NetworkConfig.Types;
using Version = FrostFS.SDK.ModelsV2.Version;
namespace FrostFS.SDK.ClientV2;
@ -20,12 +24,19 @@ public partial class Client: IFrostFSClient
public readonly OwnerId OwnerId;
public readonly Version Version = new(2, 13);
private readonly Dictionary<string, ulong> NetworkSettings = [];
private ContainerService.ContainerServiceClient? _containerServiceClient;
private NetmapService.NetmapServiceClient? _netmapServiceClient;
private ObjectService.ObjectServiceClient? _objectServiceClient;
private SessionService.SessionServiceClient? _sessionServiceClient;
public Client(string key, string host)
public static IFrostFSClient GetInstance(string key, string host)
{
return new Client(key, host);
}
private Client(string key, string host)
{
// TODO: Развязать клиент и реализацию GRPC
_key = key.LoadWif();
@ -36,8 +47,35 @@ public partial class Client: IFrostFSClient
InitObjectClient();
InitSessionClient();
CheckFrostFsVersionSupport();
InitNetworkInfoAsync();
}
private async void InitNetworkInfoAsync()
{
var info = await GetNetworkInfoAsync();
foreach (var param in info.Body.NetworkInfo.NetworkConfig.Parameters)
{
SetNetworksParam(param);
}
}
private void SetNetworksParam(Parameter param)
{
var key = Encoding.UTF8.GetString(param.Key.ToByteArray());
var encodedValue = param.Value.ToByteArray();
ulong val = 0;
for (var i = encodedValue.Length - 1; i >= 0; i--)
{
val = (val << 8) + encodedValue[i];
}
NetworkSettings.Add(key, val);
}
private async void CheckFrostFsVersionSupport()
{
var localNodeInfo = await GetLocalNodeInfoAsync();
@ -78,7 +116,11 @@ public partial class Client: IFrostFSClient
throw new ArgumentException(msg);
}
_channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions { Credentials = grpcCredentials });
_channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions
{
Credentials = grpcCredentials,
HttpHandler = new System.Net.Http.HttpClientHandler()
});
}
private void InitContainerClient()
@ -100,4 +142,4 @@ public partial class Client: IFrostFSClient
{
_sessionServiceClient = new SessionService.SessionServiceClient(_channel);
}
}
}

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using FrostFS.SDK.ModelsV2;
namespace FrostFS.SDK.ClientV2.Extensions;
public static class Extensions
{
public static ModelsV2.Object SetPayloadLength(this ModelsV2.Object obj, ulong length)
{
obj.Header.PayloadLength = length;
return obj;
}
public static ModelsV2.Object AddAttribute(this ModelsV2.Object obj, string key, string value)
{
obj.AddAttribute(new ObjectAttribute(key, value));
return obj;
}
public static ModelsV2.Object AddAttribute(this ModelsV2.Object obj, ObjectAttribute attribute)
{
obj.Header.Attributes.Add(attribute);
return obj;
}
public static ModelsV2.Object AddAttributes(this ModelsV2.Object obj, IEnumerable<ObjectAttribute> attributes)
{
obj.Header.Attributes.AddRange(attributes);
return obj;
}
public static ModelsV2.Object SetSplit(this ModelsV2.Object obj, Split split)
{
obj.Header.Split = split;
return obj;
}
public static LinkObject AddChildren(this LinkObject linkObject, IEnumerable<ObjectId> objectIds)
{
linkObject.Header.Split!.Children.AddRange(objectIds);
return linkObject;
}
}

View file

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Moq.AutoMock" Version="3.5.0" />
</ItemGroup>
<ItemGroup>

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FrostFS.SDK.ModelsV2;
@ -9,13 +10,25 @@ namespace FrostFS.SDK.ClientV2.Interfaces;
public interface IFrostFSClient
{
Task<ModelsV2.Container> GetContainerAsync(ContainerId containerId);
IAsyncEnumerable<ContainerId> ListContainersAsync();
Task<ContainerId> CreateContainerAsync(ModelsV2.Container container);
Task DeleteContainerAsync(ContainerId containerId);
Task<ObjectHeader> GetObjectHeadAsync(ContainerId containerId, ObjectId objectId);
Task<ModelsV2.Object> GetObjectAsync(ContainerId containerId, ObjectId objectId);
Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload);
Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload);
Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload, CancellationToken cancellationToken = default);
Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload, CancellationToken cancellationToken = default);
Task<ObjectId> PutSingleObjectAsync(ModelsV2.Object obj, CancellationToken cancellationToken = default);
Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId);
IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters);
IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters);
}

View file

@ -1,4 +1,5 @@
using FrostFS.Refs;
using FrostFS.SDK.Cryptography;
using FrostFS.SDK.ModelsV2;
using Google.Protobuf;
@ -10,7 +11,7 @@ public static class ContainerIdMapper
{
return new ContainerID
{
Value = ByteString.CopyFrom(containerId.ToHash())
Value = ByteString.CopyFrom(Base58.Decode(containerId.Value))
};
}
}

View file

@ -1,8 +1,9 @@
using System;
using System.Linq;
using FrostFS.Object;
using FrostFS.SDK.Cryptography;
using FrostFS.SDK.ModelsV2;
using Google.Protobuf;
using MatchType = FrostFS.Object.MatchType;
using ObjectType = FrostFS.Object.ObjectType;
@ -29,11 +30,20 @@ public static class ObjectFilterMapper
{
public static SearchRequest.Types.Body.Types.Filter ToGrpcMessage(this ObjectFilter filter)
{
var objMatchTypeName = Enum.GetName(typeof(MatchType), filter.MatchType)
?? throw new ArgumentException($"Unknown MatchType. Value: '{filter.MatchType}'.");
var objMatchTypeName = filter.MatchType switch
{
ModelsV2.Enums.ObjectMatchType.Unspecified => MatchType.Unspecified,
ModelsV2.Enums.ObjectMatchType.Equals => MatchType.StringEqual,
ModelsV2.Enums.ObjectMatchType.NotEquals => MatchType.StringNotEqual,
ModelsV2.Enums.ObjectMatchType.KeyAbsent => MatchType.NotPresent,
ModelsV2.Enums.ObjectMatchType.StartsWith => MatchType.CommonPrefix,
_ => throw new ArgumentException($"Unknown MatchType. Value: '{filter.MatchType}'.")
};
return new SearchRequest.Types.Body.Types.Filter
{
MatchType = (MatchType)Enum.Parse(typeof(MatchType), objMatchTypeName),
MatchType = objMatchTypeName,
Key = filter.Key,
Value = filter.Value
};
@ -44,13 +54,19 @@ public static class ObjectHeaderMapper
{
public static Header ToGrpcMessage(this ObjectHeader header)
{
var objTypeName = Enum.GetName(typeof(ObjectType), header.ObjectType)
?? throw new ArgumentException($"Unknown ObjectType. Value: '{header.ObjectType}'.");
var head = new Header
var objTypeName = header.ObjectType switch
{
Attributes = { },
ModelsV2.Enums.ObjectType.Regular => ObjectType.Regular,
ModelsV2.Enums.ObjectType.Lock => ObjectType.Lock,
ModelsV2.Enums.ObjectType.Tombstone => ObjectType.Tombstone,
_ => throw new ArgumentException($"Unknown ObjectType. Value: '{header.ObjectType}'.")
};
var head = new Header
{
ContainerId = header.ContainerId.ToGrpcMessage(),
ObjectType = (ObjectType)Enum.Parse(typeof(ObjectType), objTypeName)
ObjectType = objTypeName,
PayloadLength = header.PayloadLength
};
foreach (var attribute in header.Attributes)
@ -58,22 +74,60 @@ public static class ObjectHeaderMapper
head.Attributes.Add(attribute.ToGrpcMessage());
}
var split = header.Split;
if (split != null)
{
head.Split = new Header.Types.Split
{
Parent = split.Parent?.ToGrpcMessage(),
ParentSignature = split.ParentSignature?.ToGrpcMessage(),
ParentHeader = split.ParentHeader?.ToGrpcMessage(),
Previous = split.Previous?.ToGrpcMessage(),
SplitId = split.SplitId != null ? ByteString.CopyFrom(split.SplitId.ToBinary()) : null
};
if (split.Children != null && split.Children.Any())
head.Split.Children.AddRange(split.Children.Select(id => id.ToGrpcMessage()));
}
return head;
}
public static ObjectHeader ToModel(this Header header)
{
var objTypeName = Enum.GetName(typeof(ModelsV2.Enums.ObjectType), header.ObjectType)
?? throw new ArgumentException($"Unknown ObjectType. Value: '{header.ObjectType}'.");
return new ObjectHeader(
ContainerId.FromHash(header.ContainerId.Value.ToByteArray()),
(ModelsV2.Enums.ObjectType)Enum.Parse(typeof(ModelsV2.Enums.ObjectType), objTypeName),
var objTypeName = header.ObjectType switch
{
ObjectType.Regular => ModelsV2.Enums.ObjectType.Regular,
ObjectType.Lock => ModelsV2.Enums.ObjectType.Lock,
ObjectType.Tombstone => ModelsV2.Enums.ObjectType.Tombstone,
_ => throw new ArgumentException($"Unknown ObjectType. Value: '{header.ObjectType}'.")
};
var model = new ObjectHeader(
new ContainerId(Base58.Encode(header.ContainerId.Value.ToByteArray())),
objTypeName,
header.Attributes.Select(attribute => attribute.ToModel()).ToArray()
)
{
Size = (long)header.PayloadLength,
Version = header.Version.ToModel()
PayloadLength = header.PayloadLength,
Version = header.Version.ToModel(),
OwnerId = header.OwnerId.ToModel()
};
if (header.Split != null)
{
model.Split = new Split(SplitId.CrateFromBinary(header.Split.SplitId.ToByteArray()))
{
Parent = header.Split.Parent?.ToModel(),
ParentHeader = header.Split.ParentHeader?.ToModel(),
Previous = header.Split.Previous?.ToModel()
};
if (header.Split.Children.Any())
model.Split.Children.AddRange(header.Split.Children.Select(x => x.ToModel()));
}
return model;
}
}
@ -81,11 +135,32 @@ public static class ObjectMapper
{
public static ModelsV2.Object ToModel(this Object.Object obj)
{
return new ModelsV2.Object
return new ModelsV2.Object()
{
Header = obj.Header.ToModel(),
ObjectId = ObjectId.FromHash(obj.ObjectId.Value.ToByteArray()),
Payload = obj.Payload.ToByteArray()
};
}
}
public static class SignatureMapper
{
public static Refs.Signature ToGrpcMessage(this Signature signature)
{
var scheme = signature.Scheme switch
{
SignatureScheme.EcdsaRfc6979Sha256 => Refs.SignatureScheme.EcdsaRfc6979Sha256,
SignatureScheme.EcdsaRfc6979Sha256WalletConnect => Refs.SignatureScheme.EcdsaRfc6979Sha256WalletConnect,
SignatureScheme.EcdsaSha512 => Refs.SignatureScheme.EcdsaSha512,
_ => throw new ArgumentException(message: $"Unexpected enum value: {signature.Scheme}", paramName: nameof(signature.Scheme))
};
return new Refs.Signature
{
Key = ByteString.CopyFrom(signature.Key),
Scheme = scheme,
Sign = ByteString.CopyFrom(signature.Sign)
};
}
}
}

View file

@ -13,4 +13,9 @@ public static class ObjectIdMapper
Value = ByteString.CopyFrom(objectId.ToHash())
};
}
public static ObjectId ToModel(this ObjectID objectId)
{
return ObjectId.FromHash(objectId.Value.ToByteArray());
}
}

View file

@ -13,4 +13,9 @@ public static class OwnerIdMapper
Value = ByteString.CopyFrom(ownerId.ToHash())
};
}
public static OwnerId ToModel(this OwnerID ownerId)
{
return new OwnerId(ownerId.ToString());
}
}

View file

@ -91,7 +91,7 @@ public static class RequestSigner
{
IRequest => new RequestVerificationHeader(),
IResponse => new ResponseVerificationHeader(),
_ => throw new InvalidOperationException("Unsopported message type")
_ => throw new InvalidOperationException("Unsupported message type")
};
var verifyOrigin = message.GetVerificationHeader();

View file

@ -1,5 +1,6 @@
using FrostFS.Container;
using FrostFS.SDK.ClientV2.Mappers.GRPC;
using FrostFS.SDK.Cryptography;
using FrostFS.SDK.ModelsV2;
using System.Collections.Generic;
using System.Threading.Tasks;
@ -17,6 +18,7 @@ public partial class Client
ContainerId = cid.ToGrpcMessage()
},
};
request.AddMetaHeader();
request.Sign(_key);
var response = await _containerServiceClient.GetAsync(request);
@ -33,13 +35,14 @@ public partial class Client
OwnerId = OwnerId.ToGrpcMessage()
}
};
request.AddMetaHeader();
request.Sign(_key);
var response = await _containerServiceClient.ListAsync(request);
Verifier.CheckResponse(response);
foreach (var cid in response.Body.ContainerIds)
{
yield return ContainerId.FromHash(cid.Value.ToByteArray());
yield return new ContainerId(Base58.Encode(cid.Value.ToByteArray()));
}
}
@ -48,6 +51,7 @@ public partial class Client
var cntnr = container.ToGrpcMessage();
cntnr.OwnerId = OwnerId.ToGrpcMessage();
cntnr.Version = Version.ToGrpcMessage();
var request = new PutRequest
{
Body = new PutRequest.Types.Body
@ -60,7 +64,7 @@ public partial class Client
request.Sign(_key);
var response = await _containerServiceClient.PutAsync(request);
Verifier.CheckResponse(response);
return ContainerId.FromHash(response.Body.ContainerId.Value.ToByteArray());
return new ContainerId(Base58.Encode(response.Body.ContainerId.Value.ToByteArray()));
}
public async Task DeleteContainerAsync(ContainerId cid)

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Google.Protobuf;
@ -9,9 +10,12 @@ using FrostFS.Object;
using FrostFS.Refs;
using FrostFS.SDK.ClientV2.Mappers.GRPC;
using FrostFS.SDK.Cryptography;
using FrostFS.SDK.ModelsV2;
using FrostFS.Session;
using FrostFS.SDK.ModelsV2;
using FrostFS.SDK.ClientV2.Extensions;
using System.Threading;
namespace FrostFS.SDK.ClientV2;
public partial class Client
@ -20,7 +24,7 @@ public partial class Client
{
var request = new HeadRequest
{
Body = new HeadRequest.Types.Body
Body = new HeadRequest.Types.Body
{
Address = new Address
{
@ -29,12 +33,13 @@ public partial class Client
}
}
};
request.AddMetaHeader();
request.Sign(_key);
var response = await _objectServiceClient.HeadAsync(request);
var response = await _objectServiceClient!.HeadAsync(request);
Verifier.CheckResponse(response);
return response.Body.Header.Header.ToModel();
}
@ -45,7 +50,6 @@ public partial class Client
{
Body = new GetRequest.Types.Body
{
Raw = false,
Address = new Address
{
ContainerId = cid.ToGrpcMessage(),
@ -69,106 +73,94 @@ public partial class Client
return obj.ToModel();
}
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload)
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload, CancellationToken cancellationToken = default)
{
return await PutObject(header, payload);
return await PutObject(header, payload, cancellationToken);
}
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload)
public async Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload, CancellationToken cancellationToken = default)
{
return await PutObject(header, new MemoryStream(payload));
using var stream = new MemoryStream(payload);
return await PutObject(header, stream, cancellationToken);
}
public async Task DeleteObjectAsync(ContainerId cid, ObjectId oid)
private Task<ObjectId> PutObject(ObjectHeader header, Stream payload, CancellationToken cancellationToken)
{
var request = new DeleteRequest
{
Body = new DeleteRequest.Types.Body
{
Address = new Address
{
ContainerId = cid.ToGrpcMessage(),
ObjectId = oid.ToGrpcMessage()
}
}
};
request.AddMetaHeader();
request.Sign(_key);
var response = await _objectServiceClient.DeleteAsync(request);
Verifier.CheckResponse(response);
if (header.ClientCut)
return PutClientCutObject(header, payload, cancellationToken);
else
return PutStreamObject(header, payload, cancellationToken);
}
public async IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters)
{
var request = new SearchRequest
{
Body = new SearchRequest.Types.Body
{
ContainerId = cid.ToGrpcMessage(),
Filters = { },
Version = 1
}
};
private async Task<ObjectId> PutClientCutObject(ObjectHeader header, Stream payloadStream, CancellationToken cancellationToken)
{
ObjectId? objectId = null;
List<ObjectId> sentObjectIds = [];
ModelsV2.Object? currentObject;
foreach (var filter in filters)
{
request.Body.Filters.Add(filter.ToGrpcMessage());
}
var partSize = (int)NetworkSettings["MaxObjectSize"];
var buffer = new byte[partSize];
var largeObject = new LargeObject(header.ContainerId);
var split = new Split();
request.AddMetaHeader();
request.Sign(_key);
var objectsIds = SearchObjects(request);
await foreach (var oid in objectsIds)
var fullLength = (ulong)payloadStream.Length;
while (true)
{
yield return ObjectId.FromHash(oid.Value.ToByteArray());
}
}
cancellationToken.ThrowIfCancellationRequested();
private async Task<Object.Object> GetObject(GetRequest request)
{
using var stream = GetObjectInit(request);
var obj = await stream.ReadHeader();
var payload = new byte[obj.Header.PayloadLength];
var offset = 0;
var chunk = await stream.ReadChunk();
while (chunk is not null)
{
chunk.CopyTo(payload, offset);
offset += chunk.Length;
chunk = await stream.ReadChunk();
var bytesCount = await payloadStream.ReadAsync(buffer, 0, partSize);
split.Previous = sentObjectIds.LastOrDefault();
largeObject.AppendBlock(buffer, bytesCount);
currentObject = new ModelsV2.Object(header.ContainerId, bytesCount < partSize ? buffer.Take(bytesCount).ToArray() : buffer)
.AddAttributes(header.Attributes)
.SetSplit(split);
if (largeObject.PayloadLength == fullLength)
break;
objectId = await PutSingleObjectAsync(currentObject, cancellationToken);
sentObjectIds.Add(objectId!);
}
obj.Payload = ByteString.CopyFrom(payload);
return obj;
}
private ObjectReader GetObjectInit(GetRequest initRequest)
{
if (initRequest is null)
throw new ArgumentNullException(nameof(initRequest));
return new ObjectReader
if (sentObjectIds.Any())
{
Call = _objectServiceClient.Get(initRequest)
};
largeObject.CalculateHash();
currentObject.SetParent(largeObject);
objectId = await PutSingleObjectAsync(currentObject, cancellationToken);
sentObjectIds.Add(objectId);
var linkObject = new LinkObject(header.ContainerId, split.SplitId, largeObject)
.AddChildren(sentObjectIds);
_ = await PutSingleObjectAsync(linkObject, cancellationToken);
return currentObject.GetParentId();
}
return await PutSingleObjectAsync(currentObject, cancellationToken);
}
private async Task<ObjectId> PutObject(ObjectHeader header, Stream payload)
private async Task<ObjectId> PutStreamObject(ObjectHeader header, Stream payload, CancellationToken cancellationToken)
{
var sessionToken = await CreateSessionAsync(uint.MaxValue);
var hdr = header.ToGrpcMessage();
hdr.OwnerId = OwnerId.ToGrpcMessage();
hdr.Version = Version.ToGrpcMessage();
var oid = new ObjectID
{
Value = hdr.Sha256()
};
var request = new PutRequest
{
Body = new PutRequest.Types.Body
@ -188,23 +180,29 @@ public partial class Client
ObjectSessionContext.Types.Verb.Put,
_key
);
request.Sign(_key);
using var stream = await PutObjectInit(request);
var buffer = new byte[Constants.ObjectChunkSize];
var bufferLength = await payload.ReadAsync(buffer, 0, Constants.ObjectChunkSize);
while (bufferLength > 0)
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var bufferLength = await payload.ReadAsync(buffer, 0, Constants.ObjectChunkSize);
if (bufferLength == 0)
break;
request.Body = new PutRequest.Types.Body
{
Chunk = ByteString.CopyFrom(buffer[..bufferLength]),
};
request.VerifyHeader = null;
request.Sign(_key);
await stream.Write(request);
bufferLength = await payload.ReadAsync(buffer, 0, Constants.ObjectChunkSize);
}
var response = await stream.Close();
@ -213,6 +211,199 @@ public partial class Client
return ObjectId.FromHash(response.Body.ObjectId.Value.ToByteArray());
}
public async Task<ObjectId> PutSingleObjectAsync(ModelsV2.Object @object, CancellationToken cancellationToken = default)
{
var sessionToken = await CreateSessionAsync(uint.MaxValue);
var obj = CreateObject(@object);
var request = new PutSingleRequest
{
Body = new () { Object = obj }
};
request.AddMetaHeader();
request.AddObjectSessionToken(
sessionToken,
obj.Header.ContainerId,
obj.ObjectId,
ObjectSessionContext.Types.Verb.Put,
_key
);
request.Sign(_key);
var response = await _objectServiceClient!.PutSingleAsync(request, null, null, cancellationToken);
Verifier.CheckResponse(response);
return ObjectId.FromHash(obj.ObjectId.Value.ToByteArray());
}
public Object.Object CreateObject(ModelsV2.Object @object)
{
var grpcHeader = @object.Header.ToGrpcMessage();
grpcHeader.OwnerId = OwnerId.ToGrpcMessage();
grpcHeader.Version = Version.ToGrpcMessage();
if (@object.Payload != null)
{
grpcHeader.PayloadLength = (ulong)@object.Payload.Length;
grpcHeader.PayloadHash = Sha256Checksum(@object.Payload);
}
var split = @object.Header.Split;
if (split != null)
{
grpcHeader.Split = new Header.Types.Split
{
SplitId = split.SplitId != null ? ByteString.CopyFrom(split.SplitId.ToBinary()) : null
};
if (split.Children != null && split.Children.Any())
grpcHeader.Split.Children.AddRange(split.Children.Select(id => id.ToGrpcMessage()));
if (split.ParentHeader is not null)
{
var grpcParentHeader = CreateHeader(split.ParentHeader, []);
grpcHeader.Split.Parent = new ObjectID { Value = grpcParentHeader.Sha256() };
grpcHeader.Split.ParentHeader = grpcParentHeader;
grpcHeader.Split.ParentSignature = new Refs.Signature
{
Key = ByteString.CopyFrom(_key.PublicKey()),
Sign = ByteString.CopyFrom(_key.SignData(grpcHeader.Split.Parent.ToByteArray())),
};
split.Parent = grpcHeader.Split.Parent.ToModel();
}
grpcHeader.Split.Previous = split.Previous?.ToGrpcMessage();
}
var obj = new Object.Object
{
Header = grpcHeader,
ObjectId = new ObjectID { Value = grpcHeader.Sha256() },
Payload = ByteString.CopyFrom(@object.Payload)
};
obj.Signature = new Refs.Signature
{
Key = ByteString.CopyFrom(_key.PublicKey()),
Sign = ByteString.CopyFrom(_key.SignData(obj.ObjectId.ToByteArray())),
};
return obj;
}
public Header CreateHeader(ObjectHeader header, byte[]? payload)
{
var grpcHeader = header.ToGrpcMessage();
grpcHeader.OwnerId = OwnerId.ToGrpcMessage();
grpcHeader.Version = Version.ToGrpcMessage();
if (header.PayloadCheckSum != null)
{
grpcHeader.PayloadHash = new Checksum
{
Type = ChecksumType.Sha256,
Sum = ByteString.CopyFrom(header.PayloadCheckSum)
};
}
else
{
if (payload != null)
grpcHeader.PayloadHash = Sha256Checksum(payload);
}
return grpcHeader;
}
public async Task DeleteObjectAsync(ContainerId cid, ObjectId oid)
{
var request = new DeleteRequest
{
Body = new DeleteRequest.Types.Body
{
Address = new Address
{
ContainerId = cid.ToGrpcMessage(),
ObjectId = oid.ToGrpcMessage()
}
}
};
request.AddMetaHeader();
request.Sign(_key);
var response = await _objectServiceClient!.DeleteAsync(request);
Verifier.CheckResponse(response);
}
public async IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters)
{
var request = new SearchRequest
{
Body = new SearchRequest.Types.Body
{
ContainerId = cid.ToGrpcMessage(),
Filters = { },
Version = 1
}
};
foreach (var filter in filters)
{
request.Body.Filters.Add(filter.ToGrpcMessage());
}
request.AddMetaHeader();
request.Sign(_key);
var objectsIds = SearchObjects(request);
await foreach (var oid in objectsIds)
{
yield return ObjectId.FromHash(oid.Value.ToByteArray());
}
}
private async Task<Object.Object> GetObject(GetRequest request)
{
using var stream = GetObjectInit(request);
var obj = await stream.ReadHeader();
var payload = new byte[obj.Header.PayloadLength];
var offset = 0L;
var chunk = await stream.ReadChunk();
while (chunk is not null && (ulong)offset < obj.Header.PayloadLength)
{
var length = Math.Min((long)obj.Header.PayloadLength - offset, chunk.Length);
Array.Copy(chunk, 0, payload, offset, length);
offset += chunk.Length;
chunk = await stream.ReadChunk();
}
obj.Payload = ByteString.CopyFrom(payload);
return obj;
}
private ObjectReader GetObjectInit(GetRequest initRequest)
{
if (initRequest is null)
throw new ArgumentNullException(nameof(initRequest));
return new ObjectReader
{
Call = _objectServiceClient!.Get(initRequest)
};
}
private async Task<ObjectStreamer> PutObjectInit(PutRequest initRequest)
{
if (initRequest is null)
@ -220,9 +411,9 @@ public partial class Client
throw new ArgumentNullException(nameof(initRequest));
}
var call = _objectServiceClient.Put();
var call = _objectServiceClient!.Put();
await call.RequestStream.WriteAsync(initRequest);
return new ObjectStreamer(call);
}
@ -236,7 +427,7 @@ public partial class Client
{
yield return oid;
}
ids = await stream.Read();
}
}
@ -248,6 +439,19 @@ public partial class Client
throw new ArgumentNullException(nameof(initRequest));
}
return new SearchReader(_objectServiceClient.Search(initRequest));
return new SearchReader(_objectServiceClient!.Search(initRequest));
}
public Checksum Sha256Checksum(byte[] data)
{
return new Checksum
{
Type = ChecksumType.Sha256,
Sum = ByteString.CopyFrom(data.Sha256())
};
}
}

View file

@ -3,8 +3,6 @@ using System.Linq;
using System.Numerics;
using System.Text;
using Org.BouncyCastle.Security.Certificates;
namespace FrostFS.SDK.Cryptography;
public static class Base58

View file

@ -7,8 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="Google.Protobuf" Version="3.27.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

View file

@ -11,6 +11,7 @@ public static class Helper
internal static byte[] RIPEMD160(this byte[] value)
{
var hash = new byte[20];
var digest = new RipeMD160Digest();
digest.BlockUpdate(value, 0, value.Length);
digest.DoFinal(hash, 0);
@ -44,7 +45,6 @@ public static class Helper
{
return ByteString.CopyFrom(data.ToByteArray().Sha256());
}
public static ulong Murmur64(this byte[] value, uint seed)
{

View file

@ -1,30 +1,8 @@
using System;
namespace FrostFS.SDK.ModelsV2;
using FrostFS.SDK.Cryptography;
namespace FrostFS.SDK.ModelsV2;
public class ContainerId
public class ContainerId(string id)
{
public string Value { get; set; }
public ContainerId(string id)
{
Value = id;
}
public static ContainerId FromHash(byte[] hash)
{
if (hash.Length != Constants.Sha256HashLength)
throw new FormatException("ContainerID must be a sha256 hash.");
return new ContainerId(Base58.Encode(hash));
}
public byte[] ToHash()
{
return Base58.Decode(Value);
}
public string Value { get; set; } = id;
public override string ToString()
{

View file

@ -5,5 +5,5 @@ namespace FrostFS.SDK.ModelsV2.Netmap;
public class NodeInfo
{
public NodeState State { get; set; }
public Version Version { get; set; }
public Version? Version { get; set; }
}

View file

@ -0,0 +1,66 @@
using System.Security.Cryptography;
using FrostFS.SDK.ModelsV2.Enums;
namespace FrostFS.SDK.ModelsV2;
public class Object
{
public Object()
{
}
public Object(ContainerId container, byte[] payload, ObjectType objectType = ObjectType.Regular)
{
Payload = payload;
Header = new ObjectHeader(containerId: container, type: objectType, attributes: []);
}
public ObjectHeader Header { get; set; }
public ObjectId ObjectId { get; set; }
public byte[] Payload { get; set; }
public Signature Signature { get; set; }
public void SetParent(LargeObject largeObject)
{
Header.Split.ParentHeader = largeObject.Header;
}
public ObjectId? GetParentId()
{
return Header.Split?.Parent;
}
}
public class LargeObject(ContainerId container) : Object(container, [])
{
private SHA256 payloadHash = SHA256.Create();
public void AppendBlock(byte[] bytes, int count)
{
Header.PayloadLength += (ulong)count;
this.payloadHash.TransformBlock(bytes, 0, count, bytes, 0);
}
public LargeObject CalculateHash()
{
this.payloadHash.TransformFinalBlock([], 0, 0);
Header.PayloadCheckSum = this.payloadHash.Hash;
return this;
}
public ulong PayloadLength
{
get { return Header.PayloadLength; }
}
}
public class LinkObject : Object
{
public LinkObject(ContainerId containerId, SplitId splitId, LargeObject largeObject) : base (containerId, [])
{
Header.Split = new Split(splitId)
{
ParentHeader = largeObject.Header
};
}
}

View file

@ -0,0 +1,13 @@
namespace FrostFS.SDK.ModelsV2;
public class ObjectAttribute
{
public string Key { get; set; }
public string Value { get; set; }
public ObjectAttribute(string key, string value)
{
Key = key;
Value = value;
}
}

View file

@ -2,18 +2,6 @@ using FrostFS.SDK.ModelsV2.Enums;
namespace FrostFS.SDK.ModelsV2;
public class ObjectAttribute
{
public string Key { get; set; }
public string Value { get; set; }
public ObjectAttribute(string key, string value)
{
Key = key;
Value = value;
}
}
public class ObjectFilter
{
private const string HeaderPrefix = "$Object:";
@ -48,30 +36,3 @@ public class ObjectFilter
return new ObjectFilter(matchType, HeaderPrefix + "version", version.ToString());
}
}
public class ObjectHeader
{
public ObjectAttribute[] Attributes { get; set; }
public ContainerId ContainerId { get; set; }
public long Size { get; set; }
public ObjectType ObjectType { get; set; }
public Version Version { get; set; }
public ObjectHeader(
ContainerId containerId,
ObjectType type = ObjectType.Regular,
params ObjectAttribute[] attributes
)
{
Attributes = attributes;
ContainerId = containerId;
ObjectType = type;
}
}
public class Object
{
public ObjectHeader Header { get; set; }
public ObjectId ObjectId { get; set; }
public byte[] Payload { get; set; }
}

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using FrostFS.SDK.ModelsV2.Enums;
namespace FrostFS.SDK.ModelsV2;
public class ObjectHeader
{
public OwnerId? OwnerId { get; set; }
public List<ObjectAttribute> Attributes { get; set; }
public ContainerId ContainerId { get; set; }
public ulong PayloadLength { get; set; }
public byte[]? PayloadCheckSum { get; set; }
public ObjectType ObjectType { get; set; }
public Version? Version { get; set; }
public Split? Split { get; set; }
public bool ClientCut { get; set; }
public ObjectHeader(
ContainerId containerId,
ObjectType type = ObjectType.Regular,
params ObjectAttribute[] attributes
)
{
Attributes = [.. attributes];
ContainerId = containerId;
ObjectType = type;
}
}

View file

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
namespace FrostFS.SDK.ModelsV2;
public class Split
{
public Split() : this(new SplitId())
{
}
public Split(SplitId splitId)
{
SplitId = splitId;
}
public SplitId SplitId { get; private set; }
public ObjectId? Parent { get; set; }
public ObjectId? Previous { get; set; }
public Signature? ParentSignature { get; set; }
public ObjectHeader? ParentHeader { get; set; }
public List<ObjectId> Children { get; } = [];
}
public enum SignatureScheme {
EcdsaSha512,
EcdsaRfc6979Sha256,
EcdsaRfc6979Sha256WalletConnect
}
public class Signature
{
public byte[] Key { get; set; }
public byte[] Sign { get; set; }
public SignatureScheme Scheme { get; set; }
}
public class SplitId
{
private Guid id;
public SplitId()
{
this.id = Guid.NewGuid();
}
public SplitId(Guid guid)
{
this.id = guid;
}
private SplitId(byte[] binary)
{
this.id = new Guid(binary);
}
private SplitId(string str)
{
this.id = new Guid(str);
}
public static SplitId CrateFromBinary(byte[] binaryData)
{
return new SplitId(binaryData);
}
public static SplitId CrateFromString(string stringData)
{
return new SplitId(stringData);
}
public override string ToString()
{
return this.id.ToString();
}
public byte[]? ToBinary()
{
if (this.id == Guid.Empty)
return null;
return this.id.ToByteArray();
}
}

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using FrostFS.Session;
using Google.Protobuf;
@ -115,6 +116,62 @@ namespace FrostFS.Object
}
}
public partial class PutSingleRequest : IRequest
{
IMetaHeader IVerificableMessage.GetMetaHeader()
{
return MetaHeader;
}
IVerificationHeader IVerificableMessage.GetVerificationHeader()
{
return VerifyHeader;
}
void IVerificableMessage.SetMetaHeader(IMetaHeader metaHeader)
{
MetaHeader = (RequestMetaHeader)metaHeader;
}
void IVerificableMessage.SetVerificationHeader(IVerificationHeader verificationHeader)
{
VerifyHeader = (RequestVerificationHeader)verificationHeader;
}
public IMessage GetBody()
{
return Body;
}
}
public partial class PutSingleResponse : IResponse
{
IMetaHeader IVerificableMessage.GetMetaHeader()
{
return MetaHeader;
}
IVerificationHeader IVerificableMessage.GetVerificationHeader()
{
return VerifyHeader;
}
void IVerificableMessage.SetMetaHeader(IMetaHeader metaHeader)
{
MetaHeader = (ResponseMetaHeader)metaHeader;
}
void IVerificableMessage.SetVerificationHeader(IVerificationHeader verificationHeader)
{
VerifyHeader = (ResponseVerificationHeader)verificationHeader;
}
public IMessage GetBody()
{
return Body;
}
}
public partial class DeleteRequest : IRequest
{
IMetaHeader IVerificableMessage.GetMetaHeader()
@ -145,26 +202,31 @@ namespace FrostFS.Object
public partial class DeleteResponse : IResponse
{
[DebuggerStepThrough]
IMetaHeader IVerificableMessage.GetMetaHeader()
{
return MetaHeader;
}
[DebuggerStepThrough]
IVerificationHeader IVerificableMessage.GetVerificationHeader()
{
return VerifyHeader;
}
[DebuggerStepThrough]
void IVerificableMessage.SetMetaHeader(IMetaHeader metaHeader)
{
MetaHeader = (ResponseMetaHeader)metaHeader;
}
[DebuggerStepThrough]
void IVerificableMessage.SetVerificationHeader(IVerificationHeader verificationHeader)
{
VerifyHeader = (ResponseVerificationHeader)verificationHeader;
}
[DebuggerStepThrough]
public IMessage GetBody()
{
return Body;

View file

@ -2,7 +2,7 @@
public partial class XHeader
{
public const string ReservedXHeaderPrefix = "__NEOFS__";
public const string ReservedXHeaderPrefix = "__SYSTEM__";
public const string XHeaderNetmapEpoch = ReservedXHeaderPrefix + "NETMAP_EPOCH";
public const string XHeaderNetmapLookupDepth = ReservedXHeaderPrefix + "NETMAP_LOOKUP_DEPTH";
}

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq.AutoMock" Version="3.5.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FrostFS.SDK.ClientV2\FrostFS.SDK.ClientV2.csproj" />
</ItemGroup>
</Project>