gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:31:56 34 4
gpt4 key购买 nike

我有一个名为 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)
}

希望有帮助。

关于azure - Azure Blob Store 如何处理以 .gz 结尾的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44273642/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com