Skip to content

Commit 8fcda76

Browse files
committed
Add wildcard cors feature, update readme.
1 parent cc9bb10 commit 8fcda76

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

Options.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ class Options
1919
public string StorageKey { get; set; }
2020

2121
[Option('c', "connectionstring", Required = false, MutuallyExclusiveSet = "ConnectionString",
22-
HelpText = "Storage account key.")]
22+
HelpText = "Storage account connection string.")]
2323
public string ConnectionString { get; set; }
2424

2525
[OptionArray('e', "extensions", Required = true,
2626
HelpText = "Extensions to operate on. [.js, .css, .dat]")]
2727
public string[] Extensions { get; set; }
2828

29-
[Option('r', "replace", Required = false, DefaultValue = false,
29+
[Option('r', "replace", Required = false, DefaultValue = false,
3030
HelpText = "Replace existing files in-place.")]
3131
public bool Replace { get; set; }
3232

3333
[Option('s', "simulate", Required = false, DefaultValue = false,
3434
HelpText = "Do everything except write to blob store.")]
3535
public bool Simulate { get; set; }
3636

37-
[Option('n', "newextension", Required = false,
37+
[Option('n', "newextension", Required = false,
3838
HelpText = "Copy file with a new postfix. [.gz]")]
3939
public string NewExtension { get; set; }
4040

@@ -46,14 +46,18 @@ class Options
4646
HelpText = "Duration for cache control max age header, in seconds. Default 2592000 (30 days).")]
4747
public int MaxAgeSeconds { get; set; }
4848

49+
[Option('w', "wildcardcors", Required = false, DefaultValue = false,
50+
HelpText = "Enable wildcard CORS for this storage account.")]
51+
public bool wildcard { get; set; }
52+
4953

5054
[HelpOption]
5155
public string GetUsage()
5256
{
5357
var help = new HelpText
5458
{
5559
Heading = new HeadingInfo("Azure Storage GZip Encoder", "1.0"),
56-
Copyright = new CopyrightInfo("Stefan Gordon", 2016),
60+
Copyright = new CopyrightInfo("Stefan Gordon", 2016),
5761
AdditionalNewLineAfterOption = true,
5862
AddDashesToOption = true
5963
};

Program.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -43,13 +44,16 @@ static void Main(string[] args)
4344
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
4445
CloudBlobContainer blobContainer = blobClient.GetContainerReference(options.Container);
4546

47+
// Do the compression work
4648
Utility.EnsureGzipFiles(blobContainer, options.Extensions, options.Replace, options.NewExtension, options.MaxAgeSeconds, options.Simulate);
4749

48-
}
49-
else
50-
{
51-
// Display the default usage information
52-
//Console.WriteLine(options.GetUsage());
50+
// Enable CORS if appropriate
51+
if (options.wildcard)
52+
{
53+
Utility.SetWildcardCorsOnBlobService(storageAccount);
54+
}
55+
56+
Trace.TraceInformation("Complete.");
5357
}
5458
}
5559
}

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![Build status](https://ci.appveyor.com/api/projects/status/5b7d5wk4pwv21htt?svg=true)](https://ci.appveyor.com/project/stefangordon/azure-storage-gzip-encoding)
22

33
# Azure Storage GZip Encoding
4-
A utility to automatically configure [HTTP Compression](https://en.wikipedia.org/wiki/HTTP_compression) for blobs in Azure Blob storage. Blobs can be consumed directly from a client browser or via Azure CDN.
4+
A utility to automatically configure [HTTP Compression](https://en.wikipedia.org/wiki/HTTP_compression) for blobs in Azure Blob storage. Blobs can be consumed directly from a client browser or via Azure CDN.
55

66
This tool is inspired by a code sample from David Rousset for optimizing BablyonJS Assets.
77

@@ -15,6 +15,8 @@ Managing this manual compression and configuration of headers yourself would be
1515
## What it does
1616
The utility can enumerate all of the files in a container. It then filters to files matching your provided extensions. These files are compressed using GZip and the content-encoding and cache headers are configured on them so they are compatible with all browsers HTTP compression features. The tool will not alter a file which is already compressed (based on inspecting the headers), so it is safe to run multiple times to catch new files.
1717

18+
The utility can also automatically configure your storage account with wildcard CORS settings which are often desirable if serving certain types of assets through Azure CDN.
19+
1820
## Getting Started
1921
You must provide
2022
- Either an account name and key, or connection string
@@ -30,6 +32,12 @@ Replacing .css files in-place. Blobs will be replaced with compressed version a
3032
Copy .css and .js to a compressed version and append a .gz extension:
3133
`asge.exe -e .css .js -f myContainer -n .gz -a myStorageAccount -k <key>`
3234

35+
Replacing .js files in-place and enabling CORS for the account:
36+
`asge.exe -w -e .js -f myContainer -r -a myStorageAccount -k <key>`
37+
38+
Replacing .js files in-place using a connection string instead of host/key:
39+
`asge.exe -e .js -f myContainer -r -c <connection string>`
40+
3341
```
3442
-a, --account Storage account host. [mystorage]
3543
@@ -49,13 +57,13 @@ Copy .css and .js to a compressed version and append a .gz extension:
4957
5058
-f, --container Required. Container to search in.
5159
60+
-w, --wildcardcors Enable wildcard CORS for this storage account.
61+
5262
-x, --cacheage (Default: 2592000) Duration for cache control max
5363
age header, in seconds. Default 2592000 (30 days).
5464
5565
--help Display this help screen.
5666
```
5767

5868
## Current State
59-
Only block blobs are supported.
60-
61-
The tool has had only light testing at this point. Ensure that you have backups of all your binary data before running the tool so that you can recover in the event a blob is corrupted.
69+
Only block blobs are supported. Please ensure you have a backup of your data before running this tool against your container.

Utility.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace ASGE
1414
{
15-
class Utility
15+
static class Utility
1616
{
1717
public static void EnsureGzipFiles(CloudBlobContainer container, IEnumerable<string> extensions, bool inPlace, string newExtension, int cacheControlMaxAgeSeconds, bool simulate)
1818
{
@@ -104,16 +104,22 @@ public static void EnsureGzipFiles(CloudBlobContainer container, IEnumerable<str
104104
});
105105
}
106106

107-
108-
//acc.SetCORSPropertiesOnBlobService(cors => {
109-
// var wildcardRule = new CorsRule() { AllowedMethods = CorsHttpMethods.Get, AllowedOrigins = { "*" } };
110-
//cors.CorsRules.Add(wildcardRule);
111-
// return cors;
112-
//});
107+
public static void SetWildcardCorsOnBlobService(this CloudStorageAccount storageAccount)
108+
{
109+
storageAccount.SetCORSPropertiesOnBlobService(cors =>
110+
{
111+
var wildcardRule = new CorsRule() { AllowedMethods = CorsHttpMethods.Get, AllowedOrigins = { "*" } };
112+
cors.CorsRules.Clear();
113+
cors.CorsRules.Add(wildcardRule);
114+
return cors;
115+
});
116+
}
113117

114118
public static void SetCORSPropertiesOnBlobService(this CloudStorageAccount storageAccount,
115119
Func<CorsProperties, CorsProperties> alterCorsRules)
116120
{
121+
Trace.TraceInformation("Configuring CORS.");
122+
117123
if (storageAccount == null || alterCorsRules == null) throw new ArgumentNullException();
118124

119125
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

0 commit comments

Comments
 (0)