forked from sacloud/usacloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_storage_get_args.go
75 lines (64 loc) · 1.54 KB
/
object_storage_get_args.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
package completion
import (
"fmt"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"github.com/sacloud/usacloud/command"
"github.com/sacloud/usacloud/command/params"
"path"
"strings"
)
func ObjectStorageGetCompleteArgs(ctx command.Context, params *params.GetObjectStorageParam, cur, prev, commandName string) {
if ctx.NArgs() != 0 {
return
}
remotePath := cur
if remotePath != "" && strings.HasPrefix(remotePath, "/") {
remotePath = strings.Replace(remotePath, "/", "", 1)
}
if remotePath != "" && !strings.HasSuffix(remotePath, "/") {
// search under current dir
remotePath = path.Dir(remotePath)
if remotePath == "." {
remotePath = ""
} else {
remotePath += "/"
}
}
// on SakuraCloud, bucket name is same as AccessKey
if params.Bucket == "" {
params.Bucket = params.AccessKey
}
auth, err := aws.GetAuth(params.AccessKey, params.SecretKey)
if err != nil {
return
}
client := s3.New(auth, aws.Region{
Name: "us-west-2",
S3Endpoint: "https://b.sakurastorage.jp",
})
bucket := client.Bucket(params.Bucket)
// list all
res, err := bucket.List(remotePath, "/", "", 0)
if err != nil {
return
}
for _, v := range res.Contents {
fmt.Println(v.Key)
}
// we search current & current+1 depth
for _, dir := range res.CommonPrefixes {
if cur != "" {
unders, err := bucket.List(dir, "/", "", 0)
if err == nil {
for _, v := range unders.Contents {
fmt.Println(v.Key)
}
for _, v := range unders.CommonPrefixes {
fmt.Println(v)
}
}
}
fmt.Println(dir)
}
}