Azure Blob Store 如何处理以 .gz 结尾的文件?

How does Azure Blob Store treat files ending in .gz?

我有一个名为 notactuallygunzipped.gz 的文件,它是一个纯文本文件,恰好以 .gz 结尾,实际上并没有压缩,看起来像这样:

1 foo bar
2 fizz buzz

我这样上传到 Azure:

az storage blob upload \
  --container-name testroot \
  --file notactuallygunzipped.gz \
  --name "gunzip/notactuallygunzipped.gz"

然后我使用 Azure Go SDK fetch the blob。我希望返回类似 1 foo bar 或任何字节格式的内容,但我是

\x1f\x8b\x08\x08\x9d\xfa-Y\x00\x03notactuallygunzipped\x003TH\xcb\xcfWHJ,\xe22RH\xca\xccKWH\xca\xcfK\xe7\x02\x00\xa5\x00\xef\x1e\x16\x00\x00\x00

如果我将文件重命名为 plaindata.txt 之类的名称,它工作正常并且我得到了我期望的结果:

'1 foo bar\n2 fizz buzz\n'

Azure 会做一些奇怪的事情吗?是自动压缩还是类似的东西?

Azure 没关系。您上传的文件 notactuallygunzipped.gz 是一个 gzip 压缩文件。你可以通过less命令读取它,默认支持解压gzip格式,看起来像纯文本,但如果使用cat,它是二进制格式。所以你需要解压通过 go package compress/gzip.

从 Azure Blob Storage 下载的 blob 的字节

作为参考,这是我使用 Go 从 Azure Blob 存储读取 gzip 文件的示例代码。

package main

import (
    "compress/gzip"
    "fmt"
    "io/ioutil"

    "github.com/Azure/azure-storage-go"
)

func main() {
    accountName := "<your-account-name>"
    accountKey := "<your-account-key>"
    client, _ := storage.NewBasicClient(accountName, accountKey)
    blobClient := client.GetBlobService()
    containerName := "mycontainer"
    container := blobClient.GetContainerReference(containerName)
    flag, _ := container.CreateIfNotExists(nil)
    fmt.Println(flag)
    blobName := "notactuallygunzipped.gz"
    blob := container.GetBlobReference(blobName)
    readCloser, _ := blob.Get(nil)
    defer readCloser.Close()
    zr, _ := gzip.NewReader(readCloser)
    content, _ := ioutil.ReadAll(zr)
    fmt.Printf("%s", content)
}

希望对您有所帮助。

BLOB - Binary Large OBject

内容或文件扩展名无关紧要。来自 Azure docs:

Azure Blob storage is a service that stores unstructured data in the cloud as objects/blobs. Blob storage can store any type of text or binary data, such as a document, media file, or application installer. Blob storage is also referred to as object storage