Created
July 10, 2012 22:05
-
-
Save chgeuer/3086522 to your computer and use it in GitHub Desktop.
Windows Azure Storage - Patch incorrect/missing MIME types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using Microsoft.WindowsAzure; | |
| using Microsoft.WindowsAzure.StorageClient; | |
| class PatchIncorrectMimeTypesProgram | |
| { | |
| static void recurse(IListBlobItem blobItem, Action<CloudBlockBlob> action) | |
| { | |
| if (blobItem is CloudBlobDirectory) | |
| { | |
| foreach (var item in ((CloudBlobDirectory)blobItem).ListBlobs()) | |
| { | |
| recurse(item, action); | |
| } | |
| } | |
| else | |
| { | |
| action((CloudBlockBlob)blobItem); | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| var mappings = new Dictionary<string, string> | |
| { | |
| { ".html", "text/html" }, | |
| { ".css", "text/css" }, | |
| { ".jpg", "image/jpeg"}, | |
| { ".jpeg", "image/jpeg"}, | |
| { ".js", "application/javascript"} | |
| }; | |
| Action<CloudBlockBlob> setMimeType = b => | |
| { | |
| var uri = b.Uri.AbsoluteUri; | |
| var mimeType = mappings.Where(x => uri.EndsWith(x.Key)).Select(x => x.Value).FirstOrDefault(); | |
| if (string.IsNullOrEmpty(mimeType)) return; | |
| if (string.Equals(b.Attributes.Properties.ContentType, mimeType)) return; | |
| b.Attributes.Properties.ContentType = mimeType; | |
| b.SetProperties(); | |
| }; | |
| var account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxxxxxx;AccountKey=Msss..."); | |
| var client = account.CreateCloudBlobClient(); | |
| var publicFiles = client.GetContainerReference("public"); | |
| publicFiles.ListBlobs().ToList().ForEach(i => recurse(i, setMimeType)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment