Files
duplicati/Duplicati/Library/Backend/S3/S3MinioClient.cs
T

232 lines
8.8 KiB
C#
Raw Normal View History

2025-01-07 09:40:39 +01:00
// Copyright (C) 2025, The Duplicati Team
2024-02-28 15:45:30 +01:00
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2019-11-22 22:37:23 +01:00
using System;
2019-11-21 22:00:23 +01:00
using System.Collections.Generic;
2019-11-21 22:37:32 +01:00
using System.IO;
2019-11-22 22:37:23 +01:00
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
2019-11-21 22:37:32 +01:00
using System.Threading;
using System.Threading.Channels;
2019-11-21 22:37:32 +01:00
using System.Threading.Tasks;
using Duplicati.Library.Common.IO;
2019-11-21 22:37:32 +01:00
using Duplicati.Library.Interface;
2019-11-24 22:53:37 +01:00
using Duplicati.Library.Utility;
2019-11-21 22:00:23 +01:00
using Minio;
using Minio.Exceptions;
namespace Duplicati.Library.Backend
{
2019-11-22 22:37:23 +01:00
public class S3MinioClient : IS3Client
2019-11-21 22:00:23 +01:00
{
2019-11-23 09:45:06 +01:00
private static readonly string Logtag = Logging.Log.LogTagFromType<S3MinioClient>();
2019-11-26 22:19:11 +01:00
private MinioClient m_client;
2019-11-25 20:52:10 +01:00
private readonly string m_locationConstraint;
private readonly string m_dnsHost;
2019-11-21 22:00:23 +01:00
2019-11-25 20:52:10 +01:00
public S3MinioClient(string awsID, string awsKey, string locationConstraint,
string servername, string storageClass, bool useSSL, Dictionary<string, string> options)
2019-11-21 22:00:23 +01:00
{
2019-11-23 09:45:06 +01:00
m_locationConstraint = locationConstraint;
2019-11-21 22:00:23 +01:00
m_client = new MinioClient(
2019-11-22 22:37:23 +01:00
servername,
2019-11-21 22:00:23 +01:00
awsID,
awsKey,
locationConstraint
2019-11-22 22:37:23 +01:00
);
if (useSSL)
{
m_client = m_client.WithSSL();
}
2019-11-25 20:52:10 +01:00
m_dnsHost = servername;
2019-11-21 22:00:23 +01:00
}
2019-11-21 22:37:32 +01:00
private static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
IObservable<T> observable,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
2019-11-21 22:37:32 +01:00
{
var channel = Channel.CreateUnbounded<T>(); // Buffered channel for async iteration
using var subscription = observable.Subscribe(
item => channel.Writer.TryWrite(item),
ex => channel.Writer.TryComplete(ex),
() => channel.Writer.TryComplete()
);
while (await channel.Reader.WaitToReadAsync(cancellationToken))
{
while (channel.Reader.TryRead(out var item))
{
yield return item;
}
}
}
public async IAsyncEnumerable<IFileEntry> ListBucketAsync(string bucketName, string prefix, bool recursive, [EnumeratorCancellation] CancellationToken cancellationToken)
{
ThrowExceptionIfBucketDoesNotExist(bucketName);
2019-11-22 22:37:23 +01:00
if (!string.IsNullOrWhiteSpace(prefix))
prefix = Util.AppendDirSeparator(prefix, "/");
var observable = m_client.ListObjectsAsync(bucketName, prefix, recursive, cancellationToken);
await foreach (var obj in ToAsyncEnumerable(observable, cancellationToken).ConfigureAwait(false))
2019-11-22 22:37:23 +01:00
{
if (obj.Key == prefix || !obj.Key.StartsWith(prefix))
continue;
yield return new FileEntry(
obj.Key.Substring(prefix.Length),
(long)obj.Size,
2019-11-22 22:37:23 +01:00
Convert.ToDateTime(obj.LastModified),
Convert.ToDateTime(obj.LastModified)
)
{ IsFolder = obj.Key.EndsWith("/") };
2019-11-22 22:37:23 +01:00
}
2019-11-21 22:37:32 +01:00
}
public async Task AddBucketAsync(string bucketName, CancellationToken cancelToken)
2019-11-21 22:37:32 +01:00
{
2019-11-23 09:45:06 +01:00
try
{
await m_client.MakeBucketAsync(bucketName, m_locationConstraint, cancelToken).ConfigureAwait(false);
2019-11-23 09:45:06 +01:00
}
catch (MinioException e)
{
Logging.Log.WriteErrorMessage(Logtag, "ErrorMakingBucketMinio", null,
"Error making bucket {0} using Minio: {1}", bucketName, e.ToString());
throw;
2019-11-23 09:45:06 +01:00
}
2019-11-21 22:37:32 +01:00
}
2024-09-29 23:04:54 +02:00
public async Task DeleteObjectAsync(string bucketName, string keyName, CancellationToken cancelToken)
2019-11-21 22:37:32 +01:00
{
2019-11-25 20:52:10 +01:00
try
{
2024-09-29 23:04:54 +02:00
await m_client.RemoveObjectAsync(bucketName, keyName, cancelToken).ConfigureAwait(false);
2019-11-25 20:52:10 +01:00
}
catch (MinioException e)
{
Logging.Log.WriteErrorMessage(Logtag, "ErrorRemovingObjectMinio", null,
"Error removing from bucket {0} object {1} using Minio: {1}",
bucketName, keyName, e.ToString());
ParseAndThrowNotFoundException(e, keyName, bucketName);
throw;
2019-11-25 20:52:10 +01:00
}
2019-11-21 22:37:32 +01:00
}
public async Task RenameFileAsync(string bucketName, string source, string target, CancellationToken cancelToken)
2019-11-21 22:37:32 +01:00
{
2019-11-25 20:52:10 +01:00
try
{
await m_client.CopyObjectAsync(bucketName, source,
bucketName, target, cancellationToken: cancelToken).ConfigureAwait(false);
2019-11-25 20:52:10 +01:00
}
catch (MinioException e)
2019-11-25 20:52:10 +01:00
{
Logging.Log.WriteErrorMessage(Logtag, "ErrorCopyingObjectMinio", null,
"Error copying object {0} to {1} in bucket {2} using Minio: {3}",
source, target, bucketName, e.ToString());
throw;
}
await DeleteObjectAsync(bucketName, source, cancelToken).ConfigureAwait(false);
2019-11-21 22:37:32 +01:00
}
public async Task GetFileStreamAsync(string bucketName, string keyName, Stream target, CancellationToken cancelToken)
2019-11-21 22:37:32 +01:00
{
2019-11-24 22:53:37 +01:00
try
{
// Check whether the object exists using statObject().
// If the object is not found, statObject() throws an exception,
// else it means that the object exists.
// Execution is successful.
await m_client.StatObjectAsync(bucketName, keyName, cancellationToken: cancelToken).ConfigureAwait(false);
2019-11-24 22:53:37 +01:00
// Get input stream to have content of 'my-objectname' from 'my-bucketname'
await m_client.GetObjectAsync(bucketName, keyName,
(stream) => { Utility.Utility.CopyStream(stream, target); }).ConfigureAwait(false);
2019-11-24 22:53:37 +01:00
}
catch (MinioException e)
{
Logging.Log.WriteErrorMessage(Logtag, "ErrorGettingObjectMinio", null,
"Error getting object {0} to {1} using Minio: {2}",
2019-11-25 20:52:10 +01:00
keyName, bucketName, e.ToString());
ParseAndThrowNotFoundException(e, keyName, bucketName);
throw;
2019-11-24 22:53:37 +01:00
}
2019-11-21 22:37:32 +01:00
}
public string GetDnsHost()
{
2019-11-25 20:52:10 +01:00
return m_dnsHost;
2019-11-21 22:37:32 +01:00
}
2019-11-25 20:52:10 +01:00
public virtual async Task AddFileStreamAsync(string bucketName, string keyName, Stream source,
CancellationToken cancelToken)
2019-11-21 22:37:32 +01:00
{
ThrowExceptionIfBucketDoesNotExist(bucketName);
2019-11-24 11:21:00 +01:00
try
{
await m_client.PutObjectAsync(bucketName,
keyName,
source,
source.Length,
"application/octet-stream", cancellationToken: cancelToken);
}
2019-11-25 20:52:10 +01:00
catch (MinioException e)
2019-11-24 11:21:00 +01:00
{
Logging.Log.WriteErrorMessage(Logtag, "ErrorPuttingObjectMinio", null,
"Error putting object {0} to {1} using Minio: {2}",
2019-11-25 20:52:10 +01:00
keyName, bucketName, e.ToString());
2019-11-24 11:21:00 +01:00
}
2019-11-21 22:37:32 +01:00
}
private void ParseAndThrowNotFoundException(MinioException e, string keyName, string bucketName)
{
if (e.ServerResponse?.StatusCode == System.Net.HttpStatusCode.NotFound || e.Response?.Code == "NoSuchKey")
throw new FileMissingException($"File {keyName} not found in bucket {bucketName}");
}
private void ThrowExceptionIfBucketDoesNotExist(string bucketName)
{
if (!m_client.BucketExistsAsync(bucketName).Await())
throw new FolderMissingException($"Bucket {bucketName} does not exist.");
}
2019-11-26 22:19:11 +01:00
#region IDisposable Members
public void Dispose()
{
m_client = null;
}
2019-11-26 22:19:11 +01:00
#endregion
2019-11-21 22:00:23 +01:00
}
}