forked from antsanchez/goS3example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
48 lines (40 loc) · 1017 Bytes
/
list.go
File metadata and controls
48 lines (40 loc) · 1017 Bytes
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
/*
List all items from an AWS S3 Bucket
*/
package main
import (
"fmt"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
)
func handlerList(w http.ResponseWriter, r *http.Request) {
svc := s3.New(sess)
input := &s3.ListObjectsInput{
Bucket: aws.String(AWS_S3_BUCKET),
}
result, err := svc.ListObjects(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
fmt.Println(s3.ErrCodeNoSuchBucket, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
showError(w, r, http.StatusBadRequest, "Something went wrong listing the files")
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "text/html")
for _, item := range result.Contents {
fmt.Fprintf(w, "<li>File %s</li>", *item.Key)
}
return
}