使用GZIP编码将CSS文件上载到blob存储

时间:2013-09-26 20:55:54

标签: vb.net azure gzip azure-storage azure-storage-blobs

我正在尝试将带有GZIP编码的css文件写入Azure blob存储。原始css正从textarea中撤出并在ccsString下面传入。正在编写该文件,我可以在Azure Management Studio中查看该文件,当我尝试在Chrome中查看css时无法找到该文件(此网页不可用)。

我显然遗漏了一些显而易见的东西,但我看不到它?

Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse("...")
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim container As CloudBlobContainer = blobClient.GetContainerReference("myContainer")

Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference("keyPath")
blockBlob.Properties.ContentType = mimeType

Dim byteArray As Byte() = Encoding.UTF8.GetBytes(ccsString)
Using memoryStream = New IO.MemoryStream(byteArray)
    Using gzip As New GZipStream(memoryStream, CompressionMode.Compress)
       blockBlob.Properties.ContentEncoding = "gzip"
       blockBlob.UploadFromStream(memoryStream)
   End Using
End Using

更新 -

我在@ Gaurav-Mantri的帮助下解决了这个问题。我也使用YUI Compressor(可用作NUGET包)来缩小我的CSS和javascript。看看它的不同之处! :)

enter image description here

1 个答案:

答案 0 :(得分:1)

请尝试此代码(抱歉,它是在C#中):

    static void Gzip()
    {
        CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("sotest");
        string dummyText = "This is a sample text which we need to compress using GZIP. This is a sample text which we need to compress using GZIP. This is a sample text which we need to compress using GZIP. This is a sample text which we need to compress using GZIP. This is a sample text which we need to compress using GZIP. ";
        dummyText += dummyText;
        dummyText += dummyText;
        dummyText += dummyText;
        dummyText += dummyText;
        dummyText += dummyText;
        dummyText += dummyText;
        CloudBlockBlob blob = container.GetBlockBlobReference("gzipcompressed.txt");
        blob.Properties.ContentEncoding = "gzip";
        blob.Properties.ContentType = "text/plain";
        var bytes = Encoding.UTF8.GetBytes(dummyText);
        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                gzip.Write(bytes, 0, bytes.Length);
            }
            ms.Position = 0;
            blob.UploadFromStream(ms);
        }
    }

现在来讨论这个问题:

我认为你的blob内容根本没有压缩gzip。如果我使用你的代码并检查blob大小,它与字节数组大小相同。现在blob没有被压缩,内容编码被设置为GZIP,因此当Chrome尝试解压缩并且失败时。我曾多次让Chrome崩溃。