Azure CloudBlobContainer.CreateIfNotExists()引发容器已存在错误并崩溃网页

时间:2019-05-02 10:47:53

标签: asp.net azure blob storage core

我有一个asp.net核心应用程序,可将文件上传到Azure Blob存储。上传成功完成,但是当CloudBlobContainer调用CreateIfNotExistAsync时,网页崩溃,并显示“容器已存在错误”。

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

 await container.CreateIfNotExistsAsync();
 return container;

我尝试了将CreateIfNotExistsAsync()与以下if一起使用

if (! await container.ExistsAsync())

但这仍然是错误的。

连接字符串中的容器名称和AccountName =小写。

我正在使用最新的稳定Microsoft.Azure.Storage.Blob NuGet软件包10.0.2

我试图赶上StroageExeception,但未调用此异常

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

我已经遍历了上一个问题中的所有要点,但是在我的场景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);
}

哪个叫

 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;
    }

0 个答案:

没有答案