Always abort multipart upload on exception

This commit is contained in:
Kieran
2022-07-27 19:41:34 +01:00
parent 5e3ceebf45
commit d7984db5bb
2 changed files with 86 additions and 75 deletions

View File

@ -19,8 +19,7 @@ public static class Extensions
RegionEndpoint = !string.IsNullOrEmpty(c.Region) ? RegionEndpoint.GetBySystemName(c.Region) : null,
ServiceURL = c.ServiceUrl?.ToString(),
UseHttp = c.ServiceUrl?.Scheme == "http",
ForcePathStyle = true,
SignatureVersion = "4"
ForcePathStyle = true
});
}

View File

@ -165,10 +165,12 @@ public class S3FileStore : StreamFileStore, IFileStore
private async Task<PrivateVoidFile> IngressMultipart(IngressPayload payload, CancellationToken cts)
{
string? uploadId;
string? uploadId = null;
var cacheKey = $"s3:{_config.Name}:multipart-upload-id:{payload.Id}";
var partsCacheKey = $"s3:{_config.Name}:multipart-upload:{payload.Id}";
try
{
if (payload.Segment == 1)
{
var mStart = new InitiateMultipartUploadRequest()
@ -208,12 +210,6 @@ public class S3FileStore : StreamFileStore, IFileStore
var bodyResponse = await _client.UploadPartAsync(mBody, cts);
if (bodyResponse.HttpStatusCode != HttpStatusCode.OK)
{
await _client.AbortMultipartUploadAsync(new()
{
BucketName = _config.BucketName,
UploadId = uploadId
}, cts);
throw new Exception("Upload aborted");
}
@ -246,6 +242,22 @@ public class S3FileStore : StreamFileStore, IFileStore
}
}
return HandleCompletedUpload(payload, segmentLength);
}
catch
{
if (uploadId != null)
{
await _client.AbortMultipartUploadAsync(new()
{
Key = payload.Id.ToString(),
BucketName = _config.BucketName,
UploadId = uploadId
}, cts);
}
throw;
}
}
}