package main import ( "fmt" "flag" "github.com/satori/go.uuid" "io/ioutil" "bytes" "mime/multipart" "net/http" "github.com/tidwall/gjson" "github.com/qiniu/api.v7/storage" "github.com/qiniu/api.v7/auth" "path" "os" "time" ) var ( accessKey = "7msMIyQ9xOjOwqO9JSPl" secretKey = "JU8kLByQ1U1Zgkfni95IC9ttvfv5NLC3qO642R1Z" bucket = "makeup-magic" ) type returnBody struct { key string hash string fsize int bucket string url string } func main() { file := flag.String("f", "test.zip", "本地文件名") flag.Parse() //file_type := path.Base(*file) file_type := path.Ext(*file) if !PathExists(*file) { nt := time.Now().Format("2006-01-02 15:04:05") fmt.Printf("[%s]文件不存在! %s\n", nt, *file) return } u1, _ := uuid.NewV4() key := u1.String() + file_type putPolicy := storage.PutPolicy{ Scope: bucket + ":" + key, ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"url":"https://makeup-magic.zone1.meitudata.com/` + key + `"}`, } mac := auth.New(accessKey, secretKey) upToken := putPolicy.UploadToken(mac) file_data, _ := ioutil.ReadFile(*file) body := new(bytes.Buffer) w := multipart.NewWriter(body) content_type := w.FormDataContentType() //form w.WriteField("token", upToken) w.WriteField("key", key) file1, _ := w.CreateFormFile("file", key) file1.Write(file_data) w.Close() //request req, _ := http.NewRequest("POST", "https://up.meitudata.com", body) req.Header.Set("Content-Type", content_type) resp, _ := http.DefaultClient.Do(req) data, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() jsonStr := string(data) url := gjson.Get(jsonStr, "url") fmt.Println(url) } func PathExists(path string) bool { _, err := os.Stat(path) if err == nil { return true } if os.IsNotExist(err) { return false } return false }