点击上方蓝字关注“汪宇杰博客”

导语

前不久我写了一篇《尝鲜新版 Azure .NET SDK》之后,使用了一段时间发现没爆,于是今天决定把博客的图片存储从旧版 SDK 迁移到新版 SDK,结果小收福报。

Content-Type

上传到 Azure Blob 的文件必须设置正确的 Content-Type (Mime Type),要不然就会加班收福报,曾经在我的《.NET Core 批量重置 Azure Blob Storage 的 mime type》解释过。因此,以下代码需要迁移到新版SDK。

var blockBlob = _container.GetBlockBlobReference(fileName);

// Why .NET Core doesn't have MimeMapping.GetMimeMapping()

string extension = Path.GetExtension(blockBlob.Uri.AbsoluteUri);

switch (extension.ToLower())

{

case ".jpg":

case ".jpeg":

blockBlob.Properties.ContentType = "image/jpeg";

break;

case ".png":

blockBlob.Properties.ContentType = "image/png";

break;

case ".gif":

blockBlob.Properties.ContentType = "image/gif";

break;

default:

break;

}

await using (var fileStream = new MemoryStream(imageBytes))

{

await blockBlob.UploadFromStreamAsync(fileStream);

}

新版 SDK 的上传方法很简单,根据 GitHub 的 Readme,一般是这样的:

// Get a reference to a blob named "sample-file" in a container named "sample-container"

BlobClient blob = container.GetBlobClient(blobName);

// Upload local file

blob.Upload(filePath);

传送门:https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/README.md#uploading-a-blob

那么 ContentType 去哪儿了?我一开始以为它现在能自动根据拓展名设置 ContentType,结果传上去的全是 application/octet-stream,爆到天上去。

新版 SDK 的文档和教程很少,Bing 没有什么卵用。996了半小时后,最终在官网的 API Reference 里找到了疑似对象:BlobHttpHeaders.ContentType

传送门:https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.models.blobhttpheaders.contenttype?WT.mc_id=AZ-MVP-5002809&view=azure-dotnet

而 BlobClientUpload 方法正好有个重载,能接受 BlobHttpHeaders 参数,于是我实验了一下,居然成功了:

BlobClient blob = _container.GetBlobClient(fileName);

// Why .NET Core doesn't have MimeMapping.GetMimeMapping()

var blobHttpHeader = new BlobHttpHeaders();

string extension = Path.GetExtension(blob.Uri.AbsoluteUri);

switch (extension.ToLower())

{

case ".jpg":

case ".jpeg":

blobHttpHeader.ContentType = "image/jpeg";

break;

case ".png":

blobHttpHeader.ContentType = "image/png";

break;

case ".gif":

blobHttpHeader.ContentType = "image/gif";

break;

default:

break;

}

await using (var fileStream = new MemoryStream(imageBytes))

{

var uploadedBlob = await blob.UploadAsync(fileStream, blobHttpHeader);

}

现在上传到 Azure Blob 里的文件能够获得正确的 Content-Type 信息。

新款 Azure .NET SDK 如何设定 Content-Type相关推荐

  1. restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type

    报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...

  2. Posted content type isn't multipart/form-data

    版权声明:欢迎转载,请注明沉默王二原创. https://blog.csdn.net/qing_gee/article/details/48712507 在有文件上传的表单提交过程中,搞不好就会报Po ...

  3. Maven报错解决:Element 'dependency' cannot have character [children], because the type's content type is

    在用maven的时候遇到报错: Element 'dependency' cannot have character [children], because the type's content ty ...

  4. 用VS向SharePoint中部署添加List 并指定应用的Content Type

    在SharePoint Project中添加相应的Item,完全用界面的形式向EricSunArticlesList中添加好对应的Column,之后用Feature将List添加到SharePoint ...

  5. Spring Boot——[Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]解决方案

    问题描述 2020-02-13 19:32:04.322 WARN 109508 --- [p-nio-80-exec-4] .m.m.a.ExceptionHandlerExceptionResol ...

  6. : Content type 'text/plain;charset=UTF-8' not supported

    : Content type 'text/plain;charset=UTF-8' not supported

  7. eclipse 设置 content type 编码格式

    前言 Eclipse Version: 2019-12 (4.14.0) 操作 打开Preferences.菜单路径为:Window–> Preferences 在搜索框中输入:content ...

  8. sendData to ABAP backend via multiple form content type

    Created by Jerry Wang, last modified on Aug 20, 2014 使用multipart/form-data的content type通过Javascript向 ...

  9. 阿里云存储OSS中设置上传文件content type

    阿里云存储OSS中设置上传文件ContentType 如果你使用浏览器上传文件,则浏览器会自动在header中设置正确地content type,然后对文件的访问会得到正确地回应. 如果采用编程的方式 ...

最新文章

  1. python可以做什么工作好-Python入门后,想要从事自由职业可以做哪方面工作?
  2. Android菜单—Menu
  3. IO多路复用:select/poll/epoll
  4. es6笔记2之解构赋值,rest和展开运算符
  5. 成功在两台电脑上安装了spark集群,mark下
  6. 下拉列表select显示ng-options
  7. 牛客网暑期ACM多校训练营(第一场)J Different Integers
  8. axis2手动设置命名空间targetNamespace
  9. COSCon'20 Apache Roadshow- China 精彩收官 | 数据篇
  10. 你们都会的防抖与节流
  11. python中nameerror怎么处理_python中的错误如何查看
  12. java二级 计算复杂利息_java 计算存款利息
  13. Python使用TCP通讯例子
  14. DDR3 MIG IP核仿真与学习
  15. Python进行高斯积分(Gaussian integral)
  16. win10浏览器闪退_win10系统打开ie11浏览器出现闪退的两种解决方法
  17. UE4学习笔记----点光源属性
  18. 解决打开WORD时提示的:“无法复制文件:无法读源文件或磁盘”
  19. 运放 采集电压 电流高端采样
  20. sdcard里边的东西能删除吗_sdcard是什么意思(小米sdcard文件可以删除吗)

热门文章

  1. 工作流与Petri net的关系
  2. CRM学习笔记(一)
  3. html邮箱地址的正则表达式,javascript写一个校验邮箱的正则表达式
  4. 如何破解您忘记的Windows密码
  5. html中怎么获取搜索框中的值,百度API 搜索框,获取相应的地点的uid
  6. 洛谷——P1305 新二叉树(新建二叉树以及遍历)
  7. 利用python实现批量查询ip地址归属地址
  8. POJ 2887 Big String
  9. Application Fundamentals
  10. 无法打开物理文件“E:\Database\VRVIES6841-FZ01-Global\VRVEIS.mdf”。操作系统错误 5:“5(拒绝访问。)”...