UploadFromByteArrayAsync 未在 asp.net 核心的 Azure Blob 存储中保存带有文件名的扩展名

UploadFromByteArrayAsync not saving extension with file name in Azure Blob storage in asp.net core

我正在将文档字节数组保存到 azure blob,但我发现扩展名没有保存。保存文件后,将返回文件的全名和扩展名。我希望当我单击 link 时,文件应该在浏览器中打开,但我却得到了..

<Error>
<Code>ResourceNotFound</Code>
<Message>The specified resource does not exist. RequestId:6012e9b1-901e-011b-57e9-447dc0000000 Time:2022-03-31T10:21:29.3337046Z</Message>
</Error>

   public async Task<string> UploadFileToBlobAsync(string strFileName, byte[] fileData, string fileMimeType)
        {
            var accessKey = _configuration.GetValue<string>("ConnectionStrings:AzureBlob");
            CloudStorageAccount csa = CloudStorageAccount.Parse(accessKey);
            CloudBlobClient cloudBlobClient = csa.CreateCloudBlobClient();
            string containerName = "api-v2-files"; //Name of your Blob Container
            CloudBlobContainer cbContainer = cloudBlobClient.GetContainerReference(containerName);

            if (await cbContainer.CreateIfNotExistsAsync())
            {
                await cbContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
            }

            if (strFileName != null && fileData != null)
            {
                CloudBlockBlob cbb = cbContainer.GetBlockBlobReference(strFileName);
                cbb.Properties.ContentType = fileMimeType;
                await cbb.UploadFromByteArrayAsync(fileData, 0, fileData.Length);
                return cbb.Uri.AbsoluteUri;
            }
            return "";
        }

问题出在以下代码行:

if (await cbContainer.CreateIfNotExistsAsync())
{
    await cbContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}

基本上,如果容器已经存在于您的存储帐户中,您的代码就不会进入 if 块。因此,您容器的访问级别不会更改。

假设您使用的是版本 11 的 SDK,请使用以下 CreateIfNotExists 的覆盖:CreateIfNotExistsAsync(BlobRequestOptions, OperationContext, CancellationToken).