场景
- minio对象存储后端
- 有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.
不知道哪里出了问题,还请大家帮帮忙。
问题解决了,Authorization里的token填错了...但是这反映出程序设计上的问题。没有好好处理resp的返回信息。