-
Notifications
You must be signed in to change notification settings - Fork 776
/
client.go
182 lines (152 loc) · 3.96 KB
/
client.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2016 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"github.com/pkg/errors"
"go.opencensus.io/plugin/ochttp"
"golang.org/x/net/context/ctxhttp"
"github.com/fission/fission/pkg/storagesvc"
)
type (
Client struct {
url string
httpClient *http.Client
}
)
// Client creates a storage service client.
func MakeClient(url string) *Client {
return &Client{
url: strings.TrimSuffix(url, "/") + "/v1",
httpClient: &http.Client{
Transport: &ochttp.Transport{},
},
}
}
// Upload sends the local file pointed to by filePath to the storage
// service, along with the metadata. It returns a file ID that can be
// used to retrieve the file.
func (c *Client) Upload(ctx context.Context, filePath string, metadata *map[string]string) (string, error) {
fi, err := os.Stat(filePath)
if err != nil {
return "", err
}
fileSize := fi.Size()
buf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(buf)
fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filePath)
if err != nil {
return "", err
}
f, err := os.Open(filePath)
if err != nil {
return "", err
}
_, err = io.Copy(fileWriter, f)
if err != nil {
return "", err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
req, err := http.NewRequest(http.MethodPost, c.url+"/archive", buf)
if err != nil {
return "", err
}
req.Header["X-File-Size"] = []string{fmt.Sprintf("%v", fileSize)}
req.Header["Content-Type"] = []string{contentType}
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
msg := fmt.Sprintf("Upload error %v", resp.Status)
return "", errors.New(msg)
}
var ur storagesvc.UploadResponse
err = json.Unmarshal(body, &ur)
if err != nil {
return "", err
}
return ur.ID, nil
}
// GetUrl returns an HTTP URL that can be used to download the file pointed to by ID
func (c *Client) GetUrl(id string) string {
return fmt.Sprintf("%v/archive?id=%v", c.url, url.PathEscape(id))
}
// Download fetches the file identified by ID to the local file path.
// filePath must not exist.
func (c *Client) Download(ctx context.Context, id string, filePath string) error {
// url for id
url := c.GetUrl(id)
// quit if file exists
_, err := os.Stat(filePath)
if err == nil || !os.IsNotExist(err) {
return errors.Errorf("file already exists: %v", filePath)
}
// create
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// make request
resp, err := ctxhttp.Get(ctx, c.httpClient, url)
if err != nil {
fmt.Println(err)
os.Remove(filePath)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
msg := fmt.Sprintf("HTTP error %v", resp.StatusCode)
os.Remove(filePath)
return errors.New(msg)
}
// download and write data
_, err = io.Copy(f, resp.Body)
if err != nil {
return err
}
return nil
}
func (c *Client) Delete(ctx context.Context, id string) error {
url := c.GetUrl(id)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("HTTP error %v", resp.StatusCode)
}
return nil
}