I need to use WebClient in a project to split a file into multiple parts and upload them in parallel. So far, I'm able to upload the parts one at a time, but am unsure as to how to upload them in parallel.
I have an UploadPart method which looks like this:
private async Task<PartETag> UploadPart(string filePath, string preSignedUrl, int partNumber)
{
WebClient wc = new();
wc.UploadProgressChanged += WebClientUploadProgressChanged;
wc.UploadFileCompleted += WebClientUploadCompleted;
_ = await wc.UploadFileTaskAsync(new Uri(preSignedUrl), "PUT", filePath);
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = wc.ResponseHeaders;
string formattedETag = myWebHeaderCollection.GetValues("ETag").FirstOrDefault().Replace(@"""", "");
PartETag partETag = new(partNumber, formattedETag);
return partETag;
}
Its called inside a foreach loop:
foreach (var part in parts)
{
var partETag = await UploadPart(part.FilePath, part.PresignedUrl, part.Number);
partETags.Add(partETag);
}
How can I modify this so that I upload parts in parallel (up to a max of 10 parts at once) while still returning the PartETag values in the response header?