经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
【Azure 存储服务】访问Azure Blob File遇见400-Condition Headers not support错误的解决之路
来源:cnblogs  作者:路边两盏灯  时间:2023/9/20 9:31:00  对本文有异议

问题描述

在微软云存储账号的服务中,存储一些静态图片,然后通过App Service访问,但是遇见了400 - condition headers not support 错误。

在单独通过浏览器访问 File Share中的文件,发现第一次可以请求成功,但是第二次刷新后就遇见400错误,第三次刷新的时候又访问成功,如此循环下去。

错误消息为:

  1. This XML file does not appear to have any style information associated with it. The document tree is shown below.
  2. <Error>
  3. <Code>ConditionHeadersNotSupported</Code>
  4. <Message>Condition headers are not supported. RequestId:cf7f3c6e-101a-0052-73db-ea03cf000000 Time:2023-09-19T09:24:55.3527405Z</Message>
  5. </Error>

 

问题解答

在网络上搜索关键字 “400 Condition headers are not supported. “, 就可以发现很多结果。

其中以 Github的结果(https://github.com/MicrosoftDocs/azure-docs/blob/main/includes/storage-files-condition-headers.md) 和 Stack Overflow (https://stackoverflow.com/questions/43706605/azure-file-storage-error-condition-headers-are-not-supported) 为参考,找到了问题的根源

 

根源

Conditional headers aren't currently supported. Applications implementing them will need to request the full file every time the file is accessed.
Storage Account 目前不支持条件标头。 实现它们的应用程序将需要在每次访问文件时请求完整的文件。

文章中也给出了解决方案,通过在上传的文件中设置 CacheContorl属性值为 no-cache, no-store, must-revalidate. 目的时告诉浏览器,不要对这个URL的内容进行缓存,必须从源站点重新验证获取最新资源。

  • 所以需要通过 Azure Storage Explorer工具对每一个文件(注意:不能对文件所属的文件夹进行修改)的属性值 【CacheControl】进行修改。

  •  在Stack Overflow中,提出了另一种解决办法,就是在每一次请求的URL后面,增加一个随机参数,以保证每次发出的请求URL不一样,避免了浏览器缓存,因此也不会添加 Conditional Header。

(Source:https://stackoverflow.com/questions/43706605/azure-file-storage-error-condition-headers-are-not-supported


经验证,这种方法是可行的。

但问题在于,这个方法也只能使用一次。

第二次刷新时(如果随即参数不变化),也会遇见400-condition headers not support报错。

 

所以最后,最好的解决办法还是在Azure Blob File Share的文件中,添加属性值 CacheContorl为 no-cache, no-store, must-revalidate。

  • 如果是新文件,可以在上传的方法中设置CacheControl Properties 。
  • 如果是已经存在的文件,可以通过PowerShell脚本批量修改文件的 CacheControl Properties, 主要是使用

1) 获取指定folder下的全部内容 az storage file list
2) Foreach 循环,如果遇见文件夹,使用递归调用,直至全部文件获取完毕
3) 对文件类型,使用 az storage file update 更改 --content-cache 值

PowerShell脚本示例如下:

  1. ## Set the Storage Account name, File Share Name, and the Acceount Key
  2. $account_name = "您的存储账号名称"
  3. $account_key = "您的存储账号密钥"
  4. $file_share_name = "需要修改的文件夹名称" #会修改文件夹中所有文件的content-cache属性值为 "no-cache, no-store, must-revalidate"
  5.  
  6. ## Recursive Call to list all files and update the file properties .
  7. Function UpdateAllFileProperties {
  8. param($foldername)
  9. Write-Host "Start to list this folder - " $foldername
  10. #List all file & folder under this input folder path...
  11. $subfiles = az storage file list -s $foldername --account-name $account_name --account-key $account_key | ConvertFrom-Json
  12. Foreach ($f in $subfiles) {
  13. If ($f.type -eq 'file') {
  14. Write-Host "\t" + $f.name
  15. #Update file properties --content-cache "no-cache, no-store, must-revalidate"
  16. az storage file update -p $f.name -s $foldername --account-name $account_name --account-key $account_key --content-cache "no-cache, no-store, must-revalidate"
  17. }
  18. elseif ($f.type -eq 'dir') {
  19. $newfolder = $foldername + '/' + $f.name
  20. Write-Host $newfolder
  21. UpdateAllFileProperties $newfolder
  22. }
  23. else {
  24. Write-Host "Invalid type, coutinue ... "
  25. }
  26. }
  27. }
  28. Write-Host "Start ... "
  29. #Start to foreach all files & folders
  30. UpdateAllFileProperties $file_share_name
  31. Write-Host "Complete ... "

 

执行结果展示动画:

 

 

参考资料

Error ConditionHeadersNotSupported from a Web Application using Azure Files from Browser : https://github.com/MicrosoftDocs/azure-docs/blob/main/includes/storage-files-condition-headers.md

Azure File Storage Error: Condition Headers Are Not Supported : https://stackoverflow.com/questions/43706605/azure-file-storage-error-condition-headers-are-not-supported

az storage file : https://learn.microsoft.com/en-us/cli/azure/storage/file?view=azure-cli-latest#az-storage-file-list()

Cache-Control : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

 
 

 

原文链接:https://www.cnblogs.com/lulight/p/17715601.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号