使用rest更新Azure CDN中的Blob内容类型

时间:2019-03-20 09:58:30

标签: c# rest azure-storage-blobs azure-blob-storage azure-cdn

我已经集成了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存储浏览器进行更改的方法,但是云中有很多pdf,因此我无法手动更改它们。

1 个答案:

答案 0 :(得分:1)

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