-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
136 lines (127 loc) · 3.29 KB
/
get.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
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tencentyun/cos-go-sdk-v5"
)
var sign bool
// main get func
func GetFunc() {
narg := flag.NArg()
if narg != 1 && narg != 2 {
ArgError()
return
}
remote := flag.Args()[0]
local := "."
sign = true
if flag.NArg() == 2 {
sign = *ove
local = flag.Args()[1]
}
fmt.Printf("\033[1;7m%-8s%-30s%s%-30s%8s%12s\033[0m\n",
"Type", "Remote", "=> ", "Local", "State ", "Size(KB)")
GetKey(remote, local)
os.Exit(0)
}
type SelfListener struct {
}
func (l *SelfListener) ProgressChangedCallback(event *cos.ProgressEvent) {
switch event.EventType {
case cos.ProgressStartedEvent:
case cos.ProgressFailedEvent:
case cos.ProgressDataEvent:
percent := event.ConsumedBytes * 100 / event.TotalBytes
fmt.Printf("%3d%%\033[4D", percent)
case cos.ProgressCompletedEvent:
}
}
func GetFile(remote string, local string, flag bool) {
// 如果文件夹存在 或者结尾为/ 识别为 文件夹
if DirString(local) || IsDir(local) {
// 如果文件夹不存在
fmt.Println(local)
if !DirExist(local) {
MkDir(local)
}
local = filepath.Join(local, filepath.Base(remote))
} else if !DirExist(filepath.Dir(local)) {
MkDir(local)
}
// 其他识别为 文件 flag 为覆盖之类
if !flag && FileExist(local) {
var ch string
fmt.Printf("\033[32m> %s 已存在,是否覆盖(y or n)?\033[0m ", local)
fmt.Scanf("%s", &ch)
fmt.Print("\033[1A")
if ch != "y" {
GetPrint(remote, local)
fmt.Print("[\033[31m ✕ \033[0m]\n")
return
}
}
GetPrint(remote, local)
opt := &cos.ObjectGetOptions{
Listener: &SelfListener{},
}
Resp, err := c.Object.GetToFile(context.Background(), remote, local, opt)
if err != nil {
fmt.Print("[\033[31m ✕ \033[0m]")
ErrorP(err)
return
}
fmt.Print("[\033[32m ✓ \033[0m]")
fmt.Printf("%12.3f\n", B2KB(Resp.ContentLength))
}
func GetKey(remote string, local string) {
if IsDir(local) && !sign {
var ch string
fmt.Printf("\033[32m> %s 已存在,是否全部覆盖(a / n / e)?\033[0m ", local)
fmt.Scanf("%s", &ch)
if ch != "n" && ch != "a" {
return
}
sign = (ch == "a")
fmt.Print("\033[1A")
}
if remote == "." {
remote = ""
}
var marker string
opt := &cos.BucketGetOptions{
Prefix: remote, // prefix表示要查询的文件夹
Delimiter: "", // deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
MaxKeys: 100, // 设置最大遍历出多少个对象, 一次listobject最大支持1000
}
// 获取目录
_, _, err := c.Bucket.Get(context.Background(), opt)
if err != nil {
fmt.Println("[\033[31m ✕ \033[0m]")
ErrorP(err)
return
}
isTruncated := true
for isTruncated {
opt.Marker = marker
v, _, err := c.Bucket.Get(context.Background(), opt)
if err != nil {
ErrorP(err)
break
}
for _, content := range v.Contents {
gremote := content.Key
rremote := strings.Replace(gremote, remote, "", -1) // 相对的路径
glocal := filepath.Join(local, rremote) // dir --> dir
if DirString(local) {
glocal += gremote[strings.LastIndexByte(gremote, '/'):]
} // file --> dir
GetFile(gremote, glocal, sign)
}
isTruncated = v.IsTruncated // 是否还有数据
marker = v.NextMarker // 设置下次请求的起始 key
}
}