0

I have a asp.net core app that uploads file to azure blob storage. The upload completes successfully but when CloudBlobContainer calls CreateIfNotExistAsync the web page crashes with a "The container already exists error".

  var container = BlobClient.GetContainerReference(containerName.ToString().ToLower());

 await container.CreateIfNotExistsAsync();
 return container;

I have tried using surronding CreateIfNotExistsAsync() with the following if

if (! await container.ExistsAsync())

but that still errors.

The container name and the AccountName= in the connection string is lowercase.

I am using the latest stable Microsoft.Azure.Storage.Blob NuGet package 10.0.2

I have tried to catch the StroageExeception but the exception is not called

 try
    {
       await container.CreateIfNotExistsAsync();
    }
    catch (StorageException ex)
    {
        Logger.LogError(ex.ToString());
    }

I have been through all the points in this previous question but none of them apply/work in my scenario Azure Blob 400 Bad request on Creation of container

public class CloudBlobStorageProvider : ICloudBlobStorageProvider
{
    private CloudBlobClient BlobClient { get; }
    private ILogger Logger { get; }

    public CloudBlobStorageProvider(IConfiguration configuration, ILogger<CloudBlobStorageProvider> logger)
    {
        var connectionString = configuration.GetConnectionString("AzureStorageAccount");

        if (!CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount))
        {
            logger.LogError($"The supplied connection string for Azure blob storage could not be parsed: {connectionString}");
        }

        BlobClient = storageAccount.CreateCloudBlobClient();
    }

    public async Task<CloudBlobContainer> GetContainerAsync(CloudBlobContainerName containerName)
    {

        var container = BlobClient.GetContainerReference(containerName.ToString().ToLower());

        await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null);

        return container;
    }
}

public interface ICloudBlobStorageProvider
{
    Task<CloudBlobContainer> GetContainerAsync(CloudBlobContainerName containerName);
}

Which is called by

 public async Task<CloudBlockBlob> UploadServiceUserAttachmentAsync(IFormFile formFile)
    {
        var fileExtenion = RegularExpressionHelpers.GetFileExtension(formFile.FileName);

        string attachmentFileName = (string.IsNullOrEmpty(fileExtenion)) ? $"{Guid.NewGuid().ToString()}" : $"{Guid.NewGuid().ToString()}{fileExtenion}";

        var userAttachmentContainer = await CloudBlobStorageProvider.GetContainerAsync(CloudBlobContainerName.userattachments);

        var blobBlockReference = userAttachmentContainer.GetBlockBlobReference(attachmentFileName);

        try
        {
            using (var stream = formFile.OpenReadStream())
            {
                await blobBlockReference.UploadFromStreamAsync(stream);
                await blobBlockReference.FetchAttributesAsync();
                var blobProperties = blobBlockReference.Properties;
                blobProperties.ContentType = formFile.ContentType;
                await blobBlockReference.SetPropertiesAsync();
            }
        }
        catch (Exception e)
        {
            Logger.LogWarning(e, $"Exception encountered while attempting to write profile photo to blob storage");

        }

        return blobBlockReference;
    }
jps
  • 20,041
  • 15
  • 75
  • 79
flowagss
  • 25
  • 8
  • 1
    Are you by any chance deleting the container before creating it? Also, please share the complete code. It seems you're calling the method to create the container after uploading whereas it should be done before that. – Gaurav Mantri May 02 '19 at 10:53
  • thank you for looking at this issue. I have included the complete code – flowagss May 02 '19 at 12:20

0 Answers0