-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
51 lines (42 loc) · 1.17 KB
/
aws.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
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func fetchFileNames() (int, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
config.WithRegion(awsRegion),
)
if err != nil {
return 0, fmt.Errorf("failed to load AWS config: %w", err)
}
ctx := context.TODO()
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String(awsEndpoint)
})
paginator := s3.NewListObjectsV2Paginator(client, &s3.ListObjectsV2Input{
Bucket: aws.String(awsBucket),
Prefix: aws.String(prefix),
})
index := 0
mapMutex.Lock()
defer mapMutex.Unlock()
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return 0, fmt.Errorf("failed to fetch bucket objects: %w", err)
}
for _, object := range page.Contents {
fileMap[index] = *object.Key
index++
}
}
log.Printf("Fetched %d files from bucket %s\n", len(fileMap), awsBucket)
return len(fileMap), nil
}