首页 > 解决方案 > 使用 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,所以我无法手动更改所有这些。

标签: c#restazure-blob-storageazure-cdn

解决方案


因此,您将要遍历容器中的 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);

推荐阅读