This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from raystack/dex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcs.go
72 lines (66 loc) · 1.73 KB
/
gcs.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
package gcs
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"github.com/goto/dex/generated/models"
)
const clientTimeout = time.Second * 120
func NewClient(keyFilePath string) (BlobObjectClient, error) {
client, err := storage.NewClient(context.Background(), option.WithCredentialsFile(keyFilePath))
if err != nil {
log.Printf("Failed to create GCSClient storageClient: %v\n", err)
return nil, err
}
return &SClient{gcsClient: client}, nil
}
func (client Client) ListDlqMetadata(bucketInfo BucketInfo) ([]models.DlqMetadata, error) {
bucket := bucketInfo.BucketName
prefix := bucketInfo.Prefix
delim := bucketInfo.Delim
ctx := context.Background()
topicDateMap := make(map[string]map[string]int64)
ctx, cancel := context.WithTimeout(ctx, clientTimeout)
defer cancel()
it := client.StorageClient.Objects(ctx, bucket, &storage.Query{
Prefix: prefix,
Delimiter: delim,
})
for {
attrs, err := it.Next()
if errors.Is(iterator.Done, err) {
break
}
if err != nil {
return nil, fmt.Errorf("Bucket(%q).Objects(): %w", bucket, err)
}
splits := strings.Split(attrs.Name, "/")
if len(splits) != 4 {
continue
}
// prefix/topic-name/date/object-name
topicName := splits[1]
date := splits[2]
if topicDateMap[topicName] == nil {
topicDateMap[topicName] = make(map[string]int64)
}
topicDateMap[topicName][date] += attrs.Size
}
var returnVal []models.DlqMetadata
for topic, dates := range topicDateMap {
for date, size := range dates {
returnVal = append(returnVal, models.DlqMetadata{
Topic: topic,
Date: date,
SizeInBytes: size,
})
}
}
return returnVal, nil
}