golang下载minio上的zip文件无法解压?

场景

  1. minio对象存储后端
  2. 有token验证

问题

使用下面的代码(从项目中抽出来的,有点简陋)

package main

import (
    "io"
    "log"
    "net/http"
    "os"
)

func main() {
    url := "http://fileserver.com/result.zip"
    client := &http.Client{}
    req, _ := http.NewRequest(http.MethodGet, url, nil)
    req.Header.Add("Content-Type", "application/octet-stream;charset=utf-8")
    req.Header.Add("Authorization", "Token aabbcc")

    resp, err := client.Do(req)
    defer resp.Body.Close()
    if err != nil {
        log.Fatal(err)
    }

    file, err := os.Create("result.zip")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    _, err = io.Copy(file, resp.Body)
    if err != nil {
        log.Fatal(err)
    }
}

如果直接在minio上点击下载result.zip文件,则可以正常解压使用。
运行上面代码,文件可以下载下来,但是使用zip命令解压报错。错误信息为:

➜  demo1 unzip result.zip
Archive:  result.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of result.zip or
        result.zip.zip, and cannot find result.zip.ZIP, period.

不知道哪里出了问题,还请大家帮帮忙。

阅读 5.8k
1 个回答

问题解决了,Authorization里的token填错了...但是这反映出程序设计上的问题。没有好好处理resp的返回信息。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题