Dev 404
Dev 404

Reputation: 1587

Azure File Storage: Error 400 (Condition headers are not supported.)

I'm not sure what is going wrong here. I am trying to display an image that is currently stored on Azure File Storage. If I go to the link directly in my browser then it seems to download just fine. But when I put the url in an img src then I am getting this error in the console.

Here is how I am currently retrieving the url to the file:

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

What exactly does it mean by "Condition headers are not supported"?

Upvotes: 2

Views: 1950

Answers (2)

Samir Baid
Samir Baid

Reputation: 1178

We faced similar issue and we started adding timestamps in the query string. As timestamp would change for each invocation, browser will not cache and hence issue will not be encountered. Though I agree blob might be a better solution esp since you use Azure AD

Upvotes: 1

Tom Sun
Tom Sun

Reputation: 24539

What exactly does it mean by "Condition headers are not supported"?

Based on my test, there is no issue in your mentioned code. According the Azure file storage Get File API, there is no specifying conditional headers supported. So if the request with If condition header,it is not accepted by Azure file server. It sometimes happens in the browser side, as browser in the some condition append the if condition header.

enter image description here

If Azure blob is acceptable, please have a try to use the Azure blob. Then it will works as expected. The get blob api that supports condition header.

This operation also supports the use of conditional headers to read the blob only if a specified condition is met. For more information, see Specifying Conditional Headers for Blob Service Operations.

Upvotes: 5

Related Questions