鍍金池/ 問答/GO  網絡安全  HTML/ golang下載minio上的zip文件無法解壓?

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.

不知道哪里出了問題,還請大家?guī)蛶兔Α?/p>

回答
編輯回答
心癌

問題解決了,Authorization里的token填錯了...但是這反映出程序設計上的問題。沒有好好處理resp的返回信息。

2017年6月24日 12:29