-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathoss.go
225 lines (193 loc) · 5.13 KB
/
oss.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/cheggaaa/pb/v3"
"github.com/valyala/fastjson"
)
var bar *pb.ProgressBar // 上传进度条
type uploadInfo struct {
Endpoint string `json:"endpoint"`
GetTokenURL string `json:"gettokenurl"`
}
type ossToken struct {
StatusCode string
AccessKeySecret string
SecurityToken string
Expiration string
AccessKeyID string `json:"AccessKeyId"`
endpoint string
}
// 进度监听
type ossProgressListener struct{}
// 实现 oss.ProgressListener 的接口
func (listener *ossProgressListener) ProgressChanged(event *oss.ProgressEvent) {
switch event.EventType {
case oss.TransferStartedEvent:
bar = pb.New64(event.TotalBytes).SetTemplate(pb.Full).Set(pb.Bytes, true).Start()
case oss.TransferDataEvent:
bar.SetCurrent(event.ConsumedBytes)
case oss.TransferCompletedEvent:
bar.Finish()
case oss.TransferFailedEvent:
bar.Finish()
default:
}
}
// 获取网页请求响应的 json
func getURLJSON(url string) (v *fastjson.Value, e error) {
defer func() {
if err := recover(); err != nil {
e = fmt.Errorf("getURLJSON() error: %v", err)
}
}()
body, err := getURL(url)
checkErr(err)
var p fastjson.Parser
v, err = p.ParseBytes(body)
checkErr(err)
return v, nil
}
// 获取 POST 表单请求响应的 json
func postFormJSON(url string, formStr string) (v *fastjson.Value, e error) {
defer func() {
if err := recover(); err != nil {
e = fmt.Errorf("postFormJSON() error: %v", err)
}
}()
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(formStr)))
checkErr(err)
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Cookie", config.Cookies)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := doRequest(req)
checkErr(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
checkErr(err)
var p fastjson.Parser
v, err = p.ParseBytes(body)
checkErr(err)
return v, nil
}
// 以 GET 请求获取网页内容
func getURL(url string) (body []byte, e error) {
defer func() {
if err := recover(); err != nil {
e = fmt.Errorf("getURL() error: %v", err)
}
}()
req, err := http.NewRequest(http.MethodGet, url, nil)
checkErr(err)
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Cookie", config.Cookies)
resp, err := doRequest(req)
checkErr(err)
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
checkErr(err)
return body, nil
}
// 获取 oss 的 token
func getOSSToken() (token *ossToken, e error) {
defer func() {
if err := recover(); err != nil {
e = fmt.Errorf("getOSSToken() error: %v", err)
}
}()
token = new(ossToken)
body, err := getURL(getinfoURL)
checkErr(err)
var info uploadInfo
err = json.Unmarshal(body, &info)
checkErr(err)
if *internal {
i := strings.Index(info.Endpoint, ".aliyuncs.com")
token.endpoint = info.Endpoint[:i] + "-internal" + info.Endpoint[i:]
} else {
token.endpoint = info.Endpoint
}
if *verbose {
log.Printf("info 的值:\n%+v", info)
}
body, err = getURL(info.GetTokenURL)
checkErr(err)
err = json.Unmarshal(body, &token)
checkErr(err)
if *verbose {
log.Printf("OSS token 的值:\n%+v", token)
}
return token, nil
}
// 获取 oss 客户端选项
func getClientOptions() (options []oss.ClientOption) {
if proxyHost != "" {
if proxyUser != "" {
options = append(options, oss.AuthProxy(proxyHost, proxyUser, proxyPassword))
} else {
options = append(options, oss.Proxy(proxyHost))
}
}
return options
}
// 利用 oss 的接口上传文件
func ossUploadFile(ft *fastToken, file string) (e error) {
defer func() {
if err := recover(); err != nil {
e = fmt.Errorf("ossUploadFile() error: %v", err)
}
}()
log.Println("普通模式上传文件:" + file)
ot, err := getOSSToken()
checkErr(err)
client, err := oss.New(ot.endpoint, ot.AccessKeyID, ot.AccessKeySecret, getClientOptions()...)
checkErr(err)
bucket, err := client.Bucket(ft.Bucket)
checkErr(err)
cb := base64.StdEncoding.EncodeToString([]byte(ft.Callback.Callback))
cbVar := base64.StdEncoding.EncodeToString([]byte(ft.Callback.CallbackVar))
options := []oss.Option{
oss.SetHeader("x-oss-security-token", ot.SecurityToken),
oss.Callback(cb),
oss.CallbackVar(cbVar),
oss.UserAgentHeader(aliUserAgent),
oss.Progress(&ossProgressListener{}),
}
fmt.Println("按 q 键停止上传并退出程序")
err = bucket.PutObjectFromFile(ft.Object, file, options...)
checkErr(err)
time.Sleep(time.Second)
// 验证上传是否成功
fileURL := fmt.Sprintf(listFileURL, config.CID, 20)
v, err := getURLJSON(fileURL)
checkErr(err)
s := string(v.GetStringBytes("data", "0", "sha"))
if s == ft.SHA1 {
log.Printf("普通模式上传 %s 成功", file)
if *removeFile {
err = remove(file)
checkErr(err)
}
} else {
panic(fmt.Errorf("普通模式上传 %s 失败", file))
}
return nil
}
// 删除文件
func remove(file string) error {
err := os.Remove(file)
if err != nil {
return fmt.Errorf("删除原文件 %s 出现错误:%w", file, err)
}
log.Printf("成功删除原文件 %s", file)
return nil
}