0

我正在尝试将具有 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。看看它带来的不同!:)

在此处输入图像描述

4

1 回答 1

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 崩溃了。

于 2013-09-27T08:42:38.823 回答