2024-04-24 09:46:03 +00:00
|
|
|
# frostfs-sdk-csharp
|
|
|
|
|
2024-04-24 11:58:22 +00:00
|
|
|
C# implementation of FrostFS SDK.
|
|
|
|
|
2024-05-02 08:18:44 +00:00
|
|
|
## Prerequisites
|
|
|
|
|
|
|
|
### Get the key for your wallet
|
|
|
|
|
|
|
|
1. Get the address
|
|
|
|
```bash
|
|
|
|
cat <path_to_your_wallet> | jq .accounts[0].address | tr -d '"'
|
|
|
|
```
|
|
|
|
|
|
|
|
2. Get the key
|
|
|
|
```bash
|
|
|
|
neo-go wallet export -w <path_to_your_wallet> -d <address_from_p1>
|
|
|
|
```
|
|
|
|
|
2024-05-21 13:14:42 +00:00
|
|
|
## Example usage
|
|
|
|
|
|
|
|
### Container
|
|
|
|
|
|
|
|
```csharp
|
2024-09-11 07:44:30 +00:00
|
|
|
using FrostFS.SDK;
|
2024-05-21 13:14:42 +00:00
|
|
|
using FrostFS.SDK.ClientV2;
|
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var Key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq";
|
|
|
|
var Host = "http://172.22.33.44:8080";
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var options = Options.Create(new SingleOwnerClientSettings
|
|
|
|
{
|
|
|
|
Key = Key,
|
|
|
|
Host = Host
|
|
|
|
});
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
using var client = Client.GetSingleOwnerInstance(options);
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
await foreach (var cid in client.ListContainersAsync())
|
|
|
|
{
|
|
|
|
await client.DeleteContainerAsync(new PrmContainerDelete(cid));
|
|
|
|
}
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var placementPolicy = new FrostFsPlacementPolicy(true, new FrostFsReplica(1));
|
2024-05-21 13:14:42 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var createContainerParam = new PrmContainerCreate(
|
|
|
|
new FrostFsContainerInfo(BasicAcl.PublicRW, new FrostFsPlacementPolicy(true, new FrostFsReplica(1))));
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var containerId = await client.CreateContainerAsync(createContainerParam);
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
using var fileStream = File.OpenRead(@"C:\Users\Paul\Pictures\cat.jpeg");
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var param = new PrmObjectPut
|
|
|
|
{
|
|
|
|
Header = new FrostFsObjectHeader(
|
|
|
|
containerId: containerId,
|
|
|
|
type: FrostFsObjectType.Regular,
|
|
|
|
[new FrostFsAttribute("fileName", "test")]),
|
|
|
|
Payload = fileStream
|
|
|
|
};
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
FrostFsObjectId objectId = await client.PutObjectAsync(param);
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-12 08:56:26 +00:00
|
|
|
var filter = new FilterByAttribute(FrostFsMatchType.Equals, "fileName", "test");
|
2024-06-10 08:31:36 +00:00
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
await foreach (var objId in client.SearchObjectsAsync(new PrmObjectSearch(containerId) { Filters = [filter] }))
|
2024-06-10 08:31:36 +00:00
|
|
|
{
|
2024-09-11 07:44:30 +00:00
|
|
|
var objHeader = await client.GetObjectHeadAsync(new PrmObjectHeadGet(containerId, objId));
|
2024-06-10 08:31:36 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 07:44:30 +00:00
|
|
|
var @object = await client.GetObjectAsync(new PrmObjectGet(containerId, objectId));
|
|
|
|
|
|
|
|
var downloadedBytes = new byte[@object.Header.PayloadLength];
|
|
|
|
MemoryStream ms = new(downloadedBytes);
|
|
|
|
|
|
|
|
ReadOnlyMemory<byte>? chunk = null;
|
|
|
|
while ((chunk = await @object.ObjectReader!.ReadChunk()) != null)
|
|
|
|
{
|
|
|
|
ms.Write(chunk.Value.Span);
|
|
|
|
}
|
|
|
|
```
|