Skip to content

Commit

Permalink
WIP on master: 0c334d1 Bump MSTest.TestFramework from 3.4.3 to 3.5.0 (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
vugarli committed Aug 9, 2024
2 parents 0c334d1 + c888eb8 commit 3893e3d
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 76 deletions.
22 changes: 15 additions & 7 deletions Minio/ApiEndpoints/ObjectOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public async Task RemoveIncompleteUploadAsync(RemoveIncompleteUploadArgs args,
{
await foreach (var upload in ListIncompleteUploadsEnumAsync(listUploadArgs, cancellationToken)
.ConfigureAwait(false))
{
if (upload.Key.Equals(args.ObjectName, StringComparison.OrdinalIgnoreCase))
{
var rmArgs = new RemoveUploadArgs()
Expand All @@ -157,6 +158,7 @@ public async Task RemoveIncompleteUploadAsync(RemoveIncompleteUploadArgs args,
.WithUploadId(upload.UploadId);
await RemoveUploadAsync(rmArgs, cancellationToken).ConfigureAwait(false);
}
}
}
catch (Exception ex) when (ex.GetType() == typeof(BucketNotFoundException))
{
Expand Down Expand Up @@ -266,8 +268,7 @@ public async Task<string> PresignedPutObjectAsync(PresignedPutObjectArgs args)
args?.Validate();
var requestMessageBuilder = await this.CreateRequest(HttpMethod.Put, args.BucketName,
args.ObjectName,
args.Headers, // contentType
Convert.ToString(args.GetType(), CultureInfo.InvariantCulture), // metaData
args.Headers,
Utils.ObjectToByteArray(args.RequestBody)).ConfigureAwait(false);
var authenticator = new V4Authenticator(Config.Secure, Config.AccessKey, Config.SecretKey, Config.Region,
Config.SessionToken);
Expand Down Expand Up @@ -402,7 +403,7 @@ public async Task<IList<DeleteError>> RemoveObjectsAsync(RemoveObjectsArgs args,
CancellationToken cancellationToken = default)
{
args?.Validate();
IList<DeleteError> errs = new List<DeleteError>();
IList<DeleteError> errs = [];
errs = args.ObjectNamesVersions.Count > 0
? await RemoveObjectVersionsHelper(args, errs.ToList(), cancellationToken).ConfigureAwait(false)
: await RemoveObjectsHelper(args, errs, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -579,8 +580,10 @@ public async Task<PutObjectResponse> PutObjectAsync(PutObjectArgs args,
var bytes = await ReadFullAsync(args.ObjectStreamData, (int)args.ObjectSize).ConfigureAwait(false);
var bytesRead = bytes.Length;
if (bytesRead != (int)args.ObjectSize)
{
throw new UnexpectedShortReadException(
$"Data read {bytesRead.ToString(CultureInfo.InvariantCulture)} is shorter than the size {args.ObjectSize.ToString(CultureInfo.InvariantCulture)} of input buffer.");
}

args = args.WithRequestBody(bytes)
.WithStreamData(null)
Expand Down Expand Up @@ -690,12 +693,13 @@ public async Task CopyObjectAsync(CopyObjectArgs args, CancellationToken cancell
(srcByteRangeSize > 0 &&
args.SourceObject.CopyOperationConditions.byteRangeEnd >=
args.SourceObjectInfo.Size))
{
throw new InvalidDataException($"Specified byte range ({args.SourceObject
.CopyOperationConditions
.byteRangeStart.ToString(CultureInfo.InvariantCulture)}-{args.SourceObject
.CopyOperationConditions.byteRangeEnd.ToString(CultureInfo.InvariantCulture)
}) does not fit within source object (size={args.SourceObjectInfo.Size
.CopyOperationConditions.byteRangeEnd.ToString(CultureInfo.InvariantCulture)}) does not fit within source object (size={args.SourceObjectInfo.Size
.ToString(CultureInfo.InvariantCulture)})");
}

if (copySize > Constants.MaxSingleCopyObjectSize ||
(srcByteRangeSize > 0 &&
Expand Down Expand Up @@ -913,7 +917,9 @@ private async Task<IDictionary<int, string>> PutObjectPartAsync(PutObjectPartArg
numPartsUploaded++;
totalParts[partNumber - 1] = new Part
{
PartNumber = partNumber, ETag = etag, Size = (long)expectedReadSize
PartNumber = partNumber,
ETag = etag,
Size = (long)expectedReadSize
};
etags[partNumber] = etag;
if (!dataToCopy.IsEmpty) progressReport.TotalBytesTransferred += dataToCopy.Length;
Expand Down Expand Up @@ -1006,7 +1012,9 @@ private async Task MultipartCopyUploadAsync(MultipartCopyUploadArgs args,

totalParts[partNumber - 1] = new Part
{
PartNumber = partNumber, ETag = cpPartResult.ETag, Size = (long)expectedReadSize
PartNumber = partNumber,
ETag = cpPartResult.ETag,
Size = (long)expectedReadSize
};
}

Expand Down
5 changes: 4 additions & 1 deletion Minio/HttpRequestMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,17 @@ public HttpRequestMessage Request

public ReadOnlyMemory<byte> Content { get; private set; }

public string ContentTypeKey => "Content-Type";
public const string ContentTypeKey = "Content-Type";
public const string HostKey = "Host";

public void AddHeaderParameter(string key, string value)
{
if (key.StartsWith("content-", StringComparison.InvariantCultureIgnoreCase) &&
!string.IsNullOrEmpty(value) &&
!BodyParameters.ContainsKey(key))
{
BodyParameters.Add(key, value);
}

HeaderParameters[key] = value;
}
Expand Down
52 changes: 32 additions & 20 deletions Minio/RequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net;
using System.Web;
using Minio.Credentials;
using Minio.DataModel;
Expand All @@ -13,8 +12,6 @@ namespace Minio;

public static class RequestExtensions
{
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings",
Justification = "This is done in the interface. String is provided here for convenience")]
public static Task<HttpResponseMessage> WrapperGetAsync(this IMinioClient minioClient, string url)
{
return minioClient is null
Expand All @@ -25,8 +22,6 @@ public static Task<HttpResponseMessage> WrapperGetAsync(this IMinioClient minioC
/// <summary>
/// Runs httpClient's PutObjectAsync method
/// </summary>
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings",
Justification = "This is done in the interface. String is provided here for convenience")]
public static Task WrapperPutAsync(this IMinioClient minioClient, string url, StreamContent strm)
{
return minioClient is null
Expand Down Expand Up @@ -98,8 +93,10 @@ private static async Task<ResponseResult> ExecuteTaskCoreAsync(this IMinioClient
.ConfigureAwait(false);
responseResult = new ResponseResult(request, response);
if (requestMessageBuilder.ResponseWriter is not null)
{
await requestMessageBuilder.ResponseWriter(responseResult.ContentStream, cancellationToken)
.ConfigureAwait(false);
}

var path = request.RequestUri.LocalPath.TrimStart('/').TrimEnd('/')
.Split('/', StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -189,18 +186,25 @@ internal static async Task<HttpRequestMessageBuilder> CreateRequest<T>(this IMin
{
ArgsCheck(args);

var contentType = "application/octet-stream";
_ = args.Headers?.TryGetValue("Content-Type", out contentType);
//var contentType = "application/octet-stream";
//_ = args.Headers?.TryGetValue("Content-Type", out contentType);
var requestMessageBuilder =
await minioClient.CreateRequest(args.RequestMethod,
args.BucketName,
args.ObjectName,
args.Headers,
contentType,
args.RequestBody).ConfigureAwait(false);
return args.BuildRequest(requestMessageBuilder);
}

private static string GetContentType(IDictionary<string, string> headerMap)
{
var contentType = "application/octet-stream";
if (headerMap is not null && headerMap.TryGetValue(HttpRequestMessageBuilder.ContentTypeKey, out var value) && !string.IsNullOrEmpty(value))
contentType = value;
return contentType;
}

/// <summary>
/// Constructs an HttpRequestMessage builder. For AWS, this function
/// has the side-effect of overriding the baseUrl in the HttpClient
Expand All @@ -211,7 +215,6 @@ await minioClient.CreateRequest(args.RequestMethod,
/// <param name="bucketName">Bucket Name</param>
/// <param name="objectName">Object Name</param>
/// <param name="headerMap">headerMap</param>
/// <param name="contentType">Content Type</param>
/// <param name="body">request body</param>
/// <param name="resourcePath">query string</param>
/// <param name="isBucketCreationRequest">boolean to define bucket creation</param>
Expand All @@ -222,11 +225,12 @@ internal static async Task<HttpRequestMessageBuilder> CreateRequest(this IMinioC
string bucketName = null,
string objectName = null,
IDictionary<string, string> headerMap = null,
string contentType = "application/octet-stream",
ReadOnlyMemory<byte> body = default,
string resourcePath = null,
bool isBucketCreationRequest = false)
{


var region = string.Empty;
if (bucketName is not null)
{
Expand Down Expand Up @@ -300,21 +304,21 @@ internal static async Task<HttpRequestMessageBuilder> CreateRequest(this IMinioC
// Append query string passed in
if (resourcePath is not null) resource += resourcePath;

HttpRequestMessageBuilder messageBuilder;
if (!string.IsNullOrEmpty(resource))
messageBuilder = new HttpRequestMessageBuilder(method, requestUrl, resource);
else
messageBuilder = new HttpRequestMessageBuilder(method, requestUrl);
var messageBuilder = !string.IsNullOrEmpty(resource)
? new HttpRequestMessageBuilder(method, requestUrl, resource)
: new HttpRequestMessageBuilder(method, requestUrl);

var contentType = GetContentType(headerMap);
if (!body.IsEmpty)
{
messageBuilder.SetBody(body);
messageBuilder.AddOrUpdateHeaderParameter("Content-Type", contentType);
messageBuilder.AddOrUpdateHeaderParameter(HttpRequestMessageBuilder.ContentTypeKey, contentType);
}

//
if (headerMap is not null)
{
if (headerMap.TryGetValue(messageBuilder.ContentTypeKey, out var value) && !string.IsNullOrEmpty(value))
headerMap[messageBuilder.ContentTypeKey] = contentType;
if (headerMap.TryGetValue(HttpRequestMessageBuilder.ContentTypeKey, out var value) && !string.IsNullOrEmpty(value))
headerMap[HttpRequestMessageBuilder.ContentTypeKey] = contentType;

foreach (var entry in headerMap) messageBuilder.AddOrUpdateHeaderParameter(entry.Key, entry.Value);
}
Expand All @@ -330,8 +334,10 @@ internal static async Task<HttpRequestMessageBuilder> CreateRequest(this IMinioC
private static void ArgsCheck(RequestArgs args)
{
if (args is null)
{
throw new ArgumentNullException(nameof(args),
"Args object cannot be null. It needs to be assigned to an instantiated child object of Args.");
}
}

/// <summary>
Expand All @@ -352,9 +358,11 @@ internal static async Task<string> GetRegion(this IMinioClient minioClient, stri

// Pick region from location HEAD request
if (rgn?.Length == 0)
{
rgn = BucketRegionCache.Instance.Exists(bucketName)
? await BucketRegionCache.Update(minioClient, bucketName).ConfigureAwait(false)
: BucketRegionCache.Instance.Region(bucketName);
}

// Defaults to us-east-1 if region could not be found
return rgn?.Length == 0 ? "us-east-1" : rgn;
Expand Down Expand Up @@ -382,10 +390,14 @@ private static void HandleIfErrorResponse(this IMinioClient minioClient, Respons
throw response.Exception;

if (handlers.Any())
{
// Run through handlers passed to take up error handling
foreach (var handler in handlers)
handler.Handle(response);
}
else
{
minioClient.DefaultErrorHandler.Handle(response);
}
}
}
Loading

0 comments on commit 3893e3d

Please sign in to comment.