Foxnouns.NET/Foxnouns.Backend/Services/ObjectStorageService.cs

37 lines
1.1 KiB
C#
Raw Normal View History

2024-09-03 16:29:51 +02:00
using Minio;
using Minio.DataModel.Args;
using Minio.Exceptions;
namespace Foxnouns.Backend.Services;
public class ObjectStorageService(ILogger logger, Config config, IMinioClient minio)
{
private readonly ILogger _logger = logger.ForContext<ObjectStorageService>();
2024-09-03 16:29:51 +02:00
public async Task RemoveObjectAsync(string path)
{
_logger.Debug("Deleting object at path {Path}", path);
2024-09-03 16:29:51 +02:00
try
{
await minio.RemoveObjectAsync(new RemoveObjectArgs().WithBucket(config.Storage.Bucket).WithObject(path));
}
catch (InvalidObjectNameException)
{
// ignore non-existent objects
2024-09-03 16:29:51 +02:00
}
}
public async Task PutObjectAsync(string path, Stream data, string contentType)
{
_logger.Debug("Putting object at path {Path} with length {Length} and content type {ContentType}", path,
data.Length, contentType);
2024-09-03 16:29:51 +02:00
await minio.PutObjectAsync(new PutObjectArgs()
.WithBucket(config.Storage.Bucket)
.WithObject(path)
.WithObjectSize(data.Length)
.WithStreamData(data)
.WithContentType(contentType)
);
}
}