【问题标题】:Update blob content type in azure cdn using rest使用 rest 更新 azure cdn 中的 blob 内容类型
【发布时间】:2019-08-10 23:39:11
【问题描述】:

我整合了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# rest azure-blob-storage azure-cdn


    【解决方案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);
    

    【讨论】:

    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 2021-10-20
    • 2021-09-17
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多