I have a azure blob that I am uploading. I do a check to see if a file is an svg image then I set the appropriate content type before uploading. I keep getting the following error "The specified blob does not exist". This is my code below:
public async Task UploadAssetsAsync(Func<GridFSFileInfo, string> prefixSelector, List<GridFSFileInfo> files, Func<GridFSFileInfo, Task<Stream>> streamOpener, Func<string, Task> progressAction)
{
if (flyersContainerClient == null)
throw new Exception("Container client not initialized. Please initialize before doing blob operations.");
var q = new Queue<Task<Response<BlobContentInfo>>>();
progressAction?.Invoke($"{files.Count}");
foreach (var f in files)
{
var pathPrefix = prefixSelector(f);
var blobClient = flyersContainerClient.GetBlobClient($"{pathPrefix}/{f.Filename.Replace("_copy", "")}");
IDictionary<string, string> metadata = new Dictionary<string, string>();
var blobhttpheader = new BlobHttpHeaders();
if (f.Filename.EndsWith("svg"))
{
blobhttpheader.ContentType = "image/svg xml";
blobClient.SetHttpHeaders(blobhttpheader);
}
var stream = await streamOpener(f);
q.Enqueue(blobClient.UploadAsync(stream, new BlobUploadOptions { HttpHeaders = blobhttpheader, TransferOptions = new Azure.Storage.StorageTransferOptions { MaximumConcurrency = 8, InitialTransferSize = 50 * 1024 * 1024 } }));
}
await Task.WhenAll(q);
}
What I have noticed is that when I am not setting httpheaders by commenting out the if statement and removing HttpHeaders = blobhttpheader,
I can upload my blob. Can someone help me with how I am setting my header and why its incorrect?
Thanks
CodePudding user response:
The reason you are getting this error is because blobClient.SetHttpHeaders(blobhttpheader);
is making an HTTP request to set the blob headers and considering the blob doesn't exist till now, you are getting Blob Not Found
error (correctly so).
To fix the error, simply remove this line of code. It is also not needed because the HTTP headers will be set when you upload the blob.