如何列出远程服务器中的文件

时间:2019-01-11 05:11:21

标签: http go https

我实现了从远程服务器下载文件的功能。

func runDownload(fileName string) error {
    url := constants.REGISTRY_URL + "/" + constants.REGISTRY_ORGANIZATION + "/" + fileName + "/2.0.0-m1"
    if cellImage == "" {
        return fmt.Errorf("no fileName specified")
    }

    dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    if err != nil {
        fmt.Println("Error in getting current directory location: " + err.Error())
        os.Exit(1)
    }
    response, downloadError := util.DownloadFile(dir+"/"+fileName, url)

    if downloadError != nil {
        fmt.Printf("\x1b[31;1m Error occurred while pulling the cell image: \x1b[0m %v \n", downloadError)
        os.Exit(1)
    }

    if response.StatusCode == 200 {
        fmt.Printf("\r\033[32m Successfully pulled fileName \033[m %s \n", util.Bold(fileName))
    }
    if response.StatusCode == 404 {
        fmt.Printf("\x1b[31;1m Error occurred while running file:\x1b[0m %v not found in registry\n", cellImage)
    }
    return nil
}

这将调用实用程序功能并下载文件。

func DownloadFile(filepath string, url string) (*http.Response, error) {
    transport := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: transport}

    resp, err := client.Get(url)
    if err != nil {
        return resp, err
    }
    defer resp.Body.Close()

    if resp.StatusCode == 200 {
        out, err := os.Create(filepath)
        if err != nil {
            return nil, err
        }
        defer out.Close()
        _, err = io.Copy(out, resp.Body)
        if err != nil {
            return nil, err
        }
    }
    return resp, nil
}

这很好,我可以下载文件。现在,我想列出上传文件的详细信息。 (名称,大小)

0 个答案:

没有答案