Azure 文件存储:错误 400(不支持条件标头。)

Qui*_*ver 2 c# azure azure-storage

我不确定这里出了什么问题。我正在尝试显示当前存储在 Azure 文件存储上的图像。如果我直接在浏览器中访问该链接,那么它似乎下载得很好。但是当我将 url 放入 then 中时,img src我在控制台中收到此错误。

以下是我当前检索文件 url 的方法:

public static string GetFile(Models.Core.Document file, string friendlyFileName = null)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    CloudFileShare share = fileClient.GetShareReference("organizations");
    CloudFileDirectory fileDirectory = share.GetRootDirectoryReference().GetDirectoryReference("Org_" + file.OrgId);

    // Get the file
    var azureFile = (CloudFile)fileDirectory.ListFilesAndDirectories().First(f => f.Uri.ToString() == file.FilePath);

    // Set up access policy so that the file can be viewed
    var sasConstraints = new SharedAccessFilePolicy();
    sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15);
    sasConstraints.Permissions = SharedAccessFilePermissions.Read;

    // Access token
    var sasFileToken = string.Empty;
    if (friendlyFileName != null){
        sasFileToken = azureFile.GetSharedAccessSignature(sasConstraints, new SharedAccessFileHeaders()
        {
            ContentDisposition = "attachment; filename=" + friendlyFileName
        });
    }
    else
    {
        sasFileToken = azureFile.GetSharedAccessSignature(sasConstraints);
    }

    // Return url to file with appended token
    return azureFile.Uri + sasFileToken;
}
Run Code Online (Sandbox Code Playgroud)

“不支持条件标头”到底是什么意思?

Tom*_*SFT 5

“不支持条件标头”到底是什么意思?

根据我的测试,您提到的代码没有问题。根据 Azure 文件存储获取文件 API不支持指定条件标头因此,如果带有If条件标头的请求,Azure 文件服务器不会接受该请求。它有时会发生在浏览器端,因为浏览器在某些情况下会附加if condition标头。

在此输入图像描述

如果 Azure blob 可以接受,请尝试使用 Azure blob。然后它将按预期工作。支持条件标头的get blob api 。

此操作还支持使用条件标头,仅在满足指定条件时才读取 blob。有关详细信息,请参阅为 Blob 服务操作指定条件标头。