Created
August 7, 2012 10:16
-
-
Save chgeuer/3284288 to your computer and use it in GitHub Desktop.
Fix the MIME types in a blob folder
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; | |
| namespace ConsoleApplication1 | |
| { | |
| class Program | |
| { | |
| 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 accountName = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT"); | |
| var key = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCESS_KEY"); | |
| var account = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, key), | |
| useHttps: true); | |
| 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