I am uploading a file to Azure Blob storage with this code.
Sometimes after calling this function I immediately try and access the blob it does not exists, and I assume the upload has not completed. How can I ensure the upload is complete? Possibly with a status of progress.
public async Task<bool> PutBlobAsync(string containerName, string blobName, byte[] content, bool allowOverwrite = false, string contentType = null)
{
try
{
var containerClient = CreateAndGetContainer(containerName);
var blobClient = containerClient.GetBlobClient(blobName);
var blobUploadOptions = new BlobUploadOptions();
if (!String.IsNullOrEmpty(contentType))
blobUploadOptions.HttpHeaders = new BlobHttpHeaders { ContentType = contentType };
if (!allowOverwrite)
blobUploadOptions.Conditions = new BlobRequestConditions { IfNoneMatch = ETag.All };
using (var stream = new MemoryStream())
{
stream.Write(content, 0, content.Length);
stream.Position = 0;
await blobClient.UploadAsync(stream, blobUploadOptions);
}
return true;
}
catch (RequestFailedException e)
{
Logger.Error(e, "PutBlobAsync");
switch (e.Status)
{
case (int)HttpStatusCode.NotFound:
case (int)HttpStatusCode.PreconditionFailed:
return false;
}
throw;
}
}


UploadAsync, callawait blobClient.ExistsAsync()in a small loop to make sure the blob is fully available/saved before accessing it.