So recently I saw that the Microsoft.Azure.Storage was deprecated and started migrating to the new Azure.Blob.Storage SDK. And everything works perfectly fine except the UploadAsync method which throws an exception on each upload.
I get a 409 The specified blob already exists error. But I am 100% sure the blob does not exist since it is always uploaded with a GUID and adding a breakpoint before the upload method and getting the BlobClient url always returns the XML from blobstorage that this blob does not exist.
The error is thrown but my file is always uploaded. I don't need the overwrite: true flag for this since it is always new.
Here is some example code:
public async Task StoreAsync(
string container,
string blob,
byte[] givenBytes,
CancellationToken cancellationToken = default)
{
BlobContainerClient containerReference = this.client.GetBlobContainerClient(container);
await containerReference.CreateIfNotExistsAsync();
BlobClient blobReference = containerReference.GetBlobClient(blob);
cancellationToken.ThrowIfCancellationRequested();
await blobReference.UploadAsync(new BinaryData(givenBytes), CancellationToken.None);
}
Note that the string blob parameter is always a GUID coming from somewhere else.
This is the complete class:
public class CustomBlobStore : IBlobStore
{
private BlobServiceClient client;
public CustomBlobStore(string accountName, string accountKey)
{
this.client = new BlobServiceClient(CreateBlobStoreConnectionString(accountName, accountKey));
}
private static string CreateBlobStoreConnectionString(string accountName, string accountKey)
{
return "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accountKey;
}
public async Task StoreAsync(
string container,
string blob,
byte[] givenBytes,
CancellationToken cancellationToken = default)
{
BlobContainerClient containerReference = this.client.GetBlobContainerClient(container);
await containerReference.CreateIfNotExistsAsync();
BlobClient blobReference = containerReference.GetBlobClient(blob);
cancellationToken.ThrowIfCancellationRequested();
await blobReference.UploadAsync(new BinaryData(givenBytes), CancellationToken.None);
}
}
This is how you would call the function(attachment can be any type of file):
public async Task<ActionResult> SaveFile(IEnumerable<HttpPostedFileBase> files)
{
var file = files.First();
byte[] fileData;
using (var ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
fileData = ms.ToArray();
}
StoreAttachmentAsync(Guid.NewGuid(),fileData);
}
private Task StoreAttachmentAsync(string name, byte[] attachment)
{
return this.customBlobStore.StoreAsync('/attachments', name, attachment);
}
The problem was in some underlying assemblies that were having version problems with different projects. On one project it was System.Buffers on the other it was System.Text.Encodings.Web.
I didn't get any error messages due to VS2019 having some exceptions turned off by default so they would never show up. I just resolved the assembly problems and everything worked as intented.
Sorry for taking up some peoples time but I did not know VS had this issue.