【问题标题】:Upload CSS file to blob storage with GZIP encoding使用 GZIP 编码将 CSS 文件上传到 Blob 存储
【发布时间】:2013-09-27 18:10:22
【问题描述】:

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

【问题讨论】:

  • 你能检查 blob 容器 ACL 吗?它应该是BlobPubic
  • 容器设置为blob,容器内其他css文件可以正常查看。

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


【解决方案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 崩溃了。

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2017-01-24
    • 2017-08-19
    • 2019-12-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多