85 lines
2.3 KiB
C#
85 lines
2.3 KiB
C#
|
using FrostFS.SDK.ClientV2;
|
||
|
using FrostFS.SDK.ModelsV2;
|
||
|
using FrostFS.SDK.ModelsV2.Enums;
|
||
|
using FrostFS.SDK.ModelsV2.Netmap;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var fsClient = new Client("KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq", "http://172.29.238.97:8080");
|
||
|
|
||
|
// List containers
|
||
|
Console.WriteLine("Existing containers:");
|
||
|
|
||
|
await foreach(var cid in fsClient.ListContainersAsync())
|
||
|
{
|
||
|
Console.WriteLine(cid.ToString());
|
||
|
// await fsClient.DeleteContainerAsync(cid);
|
||
|
// Console.WriteLine($"REmoved container: {cid}");
|
||
|
}
|
||
|
|
||
|
// Create container
|
||
|
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
|
||
|
{
|
||
|
container = await fsClient.GetContainerAsync(containerId);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
Console.WriteLine("Waiting for the container is ready");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var currentDir = Directory.GetCurrentDirectory();
|
||
|
|
||
|
// Put object
|
||
|
using var fileStream = File.OpenRead("cat.jpg");
|
||
|
|
||
|
var cat = new ObjectHeader(
|
||
|
containerId: containerId,
|
||
|
type: ObjectType.Regular,
|
||
|
new ObjectAttribute("Filename", "cat.jpg")
|
||
|
);
|
||
|
|
||
|
var objectId = await fsClient.PutObjectAsync(cat, fileStream);
|
||
|
|
||
|
// Get object header
|
||
|
var objHeader = await fsClient.GetObjectHeadAsync(containerId, objectId);
|
||
|
|
||
|
Console.WriteLine("Attributes:");
|
||
|
foreach(var attribute in objHeader.Attributes)
|
||
|
{
|
||
|
Console.WriteLine($"{attribute.Key}:{attribute.Value}");
|
||
|
}
|
||
|
|
||
|
// Get object back
|
||
|
var obj = await fsClient.GetObjectAsync(containerId, objectId);
|
||
|
|
||
|
File.WriteAllBytes("downloaded.jpg", obj.Payload);
|
||
|
|
||
|
Console.WriteLine("Existing containers after creating:");
|
||
|
await foreach(var cid in fsClient.ListContainersAsync())
|
||
|
{
|
||
|
Console.WriteLine($"ContainerId: {cid} (removing...)");
|
||
|
await fsClient.DeleteContainerAsync(cid);
|
||
|
}
|
||
|
|
||
|
await Task.Delay(2000);
|
||
|
|
||
|
await foreach(var cid in fsClient.ListContainersAsync())
|
||
|
{
|
||
|
Console.WriteLine($"Container {cid} is alive!");
|
||
|
}
|
||
|
}
|
||
|
catch(Exception ex)
|
||
|
{
|
||
|
Console.WriteLine($"Error: {ex.Message}");
|
||
|
}
|
||
|
|