Created
October 15, 2018 12:34
-
-
Save AmaranthLIS/d85ef09654384d6646e29ad037301bba to your computer and use it in GitHub Desktop.
Go upload file(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
"mime/multipart" | |
"net/http" | |
"os" | |
"strings" | |
) | |
func main() { | |
downloadFromUrl("http://localhost:8000/file/file1.bin") | |
// err := Upload("http://localhost:8001/one", "main.go") | |
// if err != nil { | |
// panic(err) | |
// } | |
// err := UploadTwo() | |
// if err != nil { | |
// panic(err) | |
// } | |
} | |
func downloadFromUrl(url string) { | |
tokens := strings.Split(url, "/") | |
fileName := tokens[len(tokens)-1] | |
fmt.Println("Downloading", url, "to", fileName) | |
// TODO: check file existence first with io.IsExist | |
output, err := os.Create(fileName) | |
if err != nil { | |
fmt.Println("Error while creating", fileName, "-", err) | |
return | |
} | |
defer output.Close() | |
response, err := http.Get(url) | |
if err != nil { | |
fmt.Println("Error while downloading", url, "-", err) | |
return | |
} | |
defer response.Body.Close() | |
n, err := io.Copy(output, response.Body) | |
if err != nil { | |
fmt.Println("Error while downloading", url, "-", err) | |
return | |
} | |
fmt.Println(n, "bytes downloaded.") | |
} | |
func Upload(url, file string) (err error) { | |
// Prepare a form that you will submit to that URL. | |
var b bytes.Buffer | |
w := multipart.NewWriter(&b) | |
// Add your image file | |
f, err := os.Open(file) | |
if err != nil { | |
return | |
} | |
defer f.Close() | |
fw, err := w.CreateFormFile("file", file) | |
if err != nil { | |
return | |
} | |
if _, err = io.Copy(fw, f); err != nil { | |
return | |
} | |
// Add the other fields | |
if fw, err = w.CreateFormField("key"); err != nil { | |
return | |
} | |
if _, err = fw.Write([]byte("KEY")); err != nil { | |
return | |
} | |
// Don't forget to close the multipart writer. | |
// If you don't close it, your request will be missing the terminating boundary. | |
w.Close() | |
// Now that you have a form, you can submit it to your handler. | |
req, err := http.NewRequest("POST", url, &b) | |
if err != nil { | |
return | |
} | |
// Don't forget to set the content type, this will contain the boundary. | |
req.Header.Set("Content-Type", w.FormDataContentType()) | |
// Submit the request | |
client := &http.Client{} | |
res, err := client.Do(req) | |
if err != nil { | |
return | |
} | |
// Check the response | |
if res.StatusCode != http.StatusOK { | |
err = fmt.Errorf("bad status: %s", res.Status) | |
} | |
return | |
} | |
func UploadTwo() (err error) { | |
url := "http://localhost:8062/many" | |
// Prepare a form that you will submit to that URL. | |
var b bytes.Buffer | |
w := multipart.NewWriter(&b) | |
for i := 1; i <= 100; i++ { | |
fname := fmt.Sprintf("file%d.bin", i) | |
fw, err2 := w.CreateFormFile("file", fname) | |
if err2 != nil { | |
err = err2 | |
return | |
} | |
f, err2 := os.Open(fname) | |
if err2 != nil { | |
err = err2 | |
return | |
} | |
if _, err2 = io.Copy(fw, f); err2 != nil { | |
err = err2 | |
return | |
} | |
f.Close() | |
} | |
// Add the other fields | |
fw, err2 := w.CreateFormField("key") | |
if err2 != nil { | |
err = err2 | |
return | |
} | |
if _, err2 = fw.Write([]byte("KEY")); err2 != nil { | |
err = err2 | |
return | |
} | |
// Don't forget to close the multipart writer. | |
// If you don't close it, your request will be missing the terminating boundary. | |
w.Close() | |
// Now that you have a form, you can submit it to your handler. | |
req, err := http.NewRequest("POST", url, &b) | |
if err != nil { | |
return | |
} | |
// Don't forget to set the content type, this will contain the boundary. | |
req.Header.Set("Content-Type", w.FormDataContentType()) | |
// Submit the request | |
client := &http.Client{} | |
res, err := client.Do(req) | |
if err != nil { | |
return | |
} | |
// Check the response | |
if res.StatusCode != http.StatusOK { | |
err = fmt.Errorf("bad status: %s", res.Status) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment