/
es.go
37 lines (34 loc) · 822 Bytes
/
es.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package service
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
_ "net/http/pprof"
)
type esData struct {
Title string `json:"title"`
HashId string `json:"hash_id"`
Length int64 `json:"length"`
CreateTime int64 `json:"create_time"`
FileType string `json:"file_type"`
Hot int `json:"hot"`
}
func EsPut(url string, data []byte) (err error) {
client := http.Client{}
req, err := http.NewRequest("PUT", url, bytes.NewReader(data))
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
resp, err := client.Do(req)
if err != nil {
return err
}
io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode != 201 && resp.StatusCode != 200 {
resp.Body.Close()
return errors.New(fmt.Sprint("error code", resp.StatusCode))
}
resp.Body.Close()
return nil
}