-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
87 lines (78 loc) · 1.83 KB
/
find.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
package piece
import (
"context"
"encoding/base64"
"fmt"
"github.com/elek/stbb/pkg/util"
"github.com/spf13/cobra"
"storj.io/uplink/private/metaclient"
"strings"
)
func init() {
cmd := &cobra.Command{
Use: "find <bucket> <storagenode>",
Short: "Find piece which is store on a specific storage nodes",
Args: cobra.ExactArgs(2),
}
dh := util.NewDialerHelper(cmd.Flags())
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
p, err := NewPieceFinder(ctx, args[1], dh)
if err != nil {
return err
}
err = p.Find(ctx, args[0])
if err != nil {
return err
}
return err
}
PieceCmd.AddCommand(cmd)
}
type PieceFinder struct {
Downloader
}
func NewPieceFinder(ctx context.Context, storagenodeID string, dh *util.DialerHelper) (PieceFinder, error) {
downloader, err := NewDownloader(ctx, storagenodeID, dh)
if err != nil {
return PieceFinder{}, err
}
return PieceFinder{
Downloader: downloader,
}, nil
}
func (p PieceFinder) Find(ctx context.Context, bucketName string) error {
dialer, err := p.dialer.CreateRPCDialer()
if err != nil {
return err
}
metainfoClient, err := metaclient.DialNodeURL(ctx,
dialer,
p.satelliteURL.String(),
p.grant.APIKey,
"stbb")
if err != nil {
return err
}
objects, _, err := metainfoClient.ListObjects(ctx, metaclient.ListObjectsParams{
Bucket: []byte(bucketName),
})
if err != nil {
return err
}
for _, o := range objects {
ips, err := metainfoClient.GetObjectIPs(ctx, metaclient.GetObjectIPsParams{
Bucket: []byte(bucketName),
EncryptedObjectKey: o.EncryptedObjectKey,
})
if err != nil {
return err
}
for _, ip := range ips.IPPorts {
if strings.Contains(string(ip), p.storagenodeURL.Address) {
fmt.Println(base64.URLEncoding.EncodeToString(o.EncryptedObjectKey))
}
}
}
return nil
}