c# - 使用 rest 更新 azure cdn 中的 blob 内容类型
问题描述
我已经集成azure cdn
并上传了很多pdf
文件,但它们都有octet-stream
内容类型,因为最初我没有使用过x-ms-blob-content-type
,现在我已经修复了它的设置
headers.Add("x-ms-blob-content-type", "application/pdf");
因此,新文件以正确的内容类型上传。我的问题是关于修复之前上传的所有 pdf 文件。我想将他们的内容类型更改为application/pdf
. 有没有办法使用rest api来做到这一点?
我找到了一种使用 azure storage explorer 更改它的方法,但是云中有很多 pdf,所以我无法手动更改所有这些。
解决方案
因此,您将要遍历容器中的 blob,如果扩展名为 .pdf,那么您希望将内容类型设置为“application/pdf”。
下面的代码应该为您指明正确的方向。
// Storage credentials
StorageCredentials credentials = new StorageCredentials("accName", "keyValue");
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("theContainer");
// Continuation Token
BlobContinuationToken token = null;
do
{
var results = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All,
null, token, null, null);
// Cast blobs to type CloudBlockBlob
var blobs = results.Results.Cast<CloudBlockBlob>().ToList();
foreach (var blob in blobs)
{
if (Path.GetExtension(blob.Uri.AbsoluteUri) == ".pdf")
{
blob.Properties.ContentType = "application/pdf";
}
await blob.SetPropertiesAsync();
}
} while (token != null);
推荐阅读
- javascript - 使用多个组件一次加载 Google 地图
- docker - 对 Windows 服务进行 Docker 化
- node.js - 如何使用猫鼬在一个nodejs查询中更改两个不同的集合
- r - 错误:数值表达式有 6 个元素:只有第一个使用的和 GA 中的警告
- angular - 将vasturiano / `sunbrust-chart` 与 Angular 2-9 集成
- ios - 无法将 Bool 类型的值分配给类型 String 数据库表示
- windows - 通过 Windows 上的文件和管道模拟键盘输入到批处理文件
- python - AWS 实时视频中的人体检测
- django - 如何使用 django 应用程序与外部服务器通信?
- django - 如何根据 Django 模型上的其他字段为字段生成 aa 值?