-
Notifications
You must be signed in to change notification settings - Fork 1
/
awss3.go
39 lines (33 loc) · 997 Bytes
/
awss3.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
package main
// Reference
// https://aws.amazon.com/blogs/developer/mocking-out-then-aws-sdk-for-go-for-unit-testing/
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
// ListObjects(*s3.ListObjectsInput) (*s3.ListObjectsOutput, error)
func ListFileNames(s3api s3iface.S3API, bucket string, prefix string) (names []string, err error) {
input := &s3.ListObjectsInput{
Bucket: aws.String(bucket),
MaxKeys: aws.Int64(10),
Prefix: aws.String(prefix),
}
output, err := s3api.ListObjects(input)
if err != nil {
return nil, err
}
names = make([]string, len(output.Contents))
for idx, content := range output.Contents {
names[idx] = *content.Key
}
return names, nil
}
func ListFileNamesExample() {
sess := session.Must(session.NewSession())
s3api := s3.New(sess)
names, _ := ListFileNames(s3api, "examplebucket", "/a/b")
fmt.Println(names)
}