Gzip writer在Golang中没有将gzip数据写入S3

huangapple go评论66阅读模式
英文:

Gzip writer not writing gzip data to S3 in Golang

问题

我有一个(希望)简单的问题。我正在尝试将HTTP请求的结果写入S3中的Gzip文件。然而,当从S3下载结果文件时,它只是普通文本,没有压缩。以下是代码片段(不包括引导部分)。代码可以构建、检查和运行,所以我不确定哪里出错了...任何指点将不胜感激!

r, w := io.Pipe()
gw := gzip.NewWriter(w)
go func() {
    defer w.Close()
    defer gw.Close()
    _, err := gw.Write(httpResponse)
    if err != nil {
        fmt.Println("error")
    }
}()

cfg, _ := config.LoadDefaultConfig(context.TODO())
s3Client := s3.NewFromConfig(cfg)
ul := manager.NewUploader(s3Client)
_, err := ul.Upload(context.TODO(), &s3.PutObjectInput{
    Bucket:          aws.String(bucket),
    ContentEncoding: aws.String("gzip"),
    Key:             aws.String(fileName),
    Body:            r,
})
if err != nil {
    fmt.Println("error")
}

希望能对你有所帮助!

英文:

I've got a (hopefully) simple problem. I'm trying to write the results of a HTTP request to a Gzip file in S3. However when downloading the resultant file from S3, it's just in plain text and not compressed. Below is a snippet of the code (sans bootstrapping). The code builds, lints and runs without error, so I'm not sure where I'm going wrong...any pointers would be greatly appreciated!

r, w := io.Pipe()
gw := gzip.NewWriter(w)
go func() {
    defer w.Close()
    defer gw.Close()
    _, err := gw.Write(httpResponse)
    if err != nil {
        fmt.Println(“error”)
    }
}()
cfg, _ := config.LoadDefaultConfig(context.TODO())
s3Client := s3.NewFromConfig(cfg)
ul := manager.NewUploader(s3Client)
_, err := ul.Upload(context.TODO(), &s3.PutObjectInput{
    Bucket:          aws.String(bucket),
    ContentEncoding: aws.String("gzip"),
    Key:             aws.String(fileName),
    Body:            r,
})
if err != nil {
    fmt.Println(“error”)
}

答案1

得分: 1

这是通过浏览器下载的副作用,它会解码gzip,但保留.gz扩展名(这实际上很令人困惑)。如果你使用AWS cli或API下载文件,它将保持为GZIP格式。

参考链接:https://superuser.com/questions/940605/chromium-prevent-unpacking-tar-gz?noredirect=1&lq=1

英文:

This is a side effect of downloading via the browser, it'll decode the gzip but leave the .gz extension (which is frankly confusing). If you use the AWS cli or API to download the file, it will remain as GZIP.

See: https://superuser.com/questions/940605/chromium-prevent-unpacking-tar-gz?noredirect=1&lq=1

huangapple
  • 本文由 发表于 2022年2月8日 09:48:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/71027664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定