I am using aws-doc-sdk-examples/go/example_code/s3
example code to list out objects in my s3 bucket.
Following two folders are created by default in bucket:
bucket-logs/AWSLogs/accountxxx/CloudTrail-Digest/
bucket-logs/AWSLogs/accountxxx/CloudTrail/
Both folders have sub folders like
/us-region-1/[year]/[month]/[day]/
I am using below GoLang code:
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
"os"
)
func main() {
if len(os.Args) != 2 {
exitErrorf("Bucket name required
Usage: %s bucket_name",
os.Args[0])
}
bucket := os.Args[1]
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
svc := s3.New(sess)
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket), })
if err != nil {
exitErrorf("Unable to list items in bucket %q, %v", bucket, err)
}
for _, item := range resp.Contents {
fmt.Println("Name: ", *item.Key)
fmt.Println("Last modified:", *item.LastModified)
fmt.Println("Size: ", *item.Size)
fmt.Println("Storage class:", *item.StorageClass)
fmt.Println("")
}
fmt.Println("Found", len(resp.Contents), "items in bucket", bucket)
fmt.Println("")
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"
", args...)
os.Exit(1)
}
The above code always returning 1000 object from bucket-logs/AWSLogs/accountxxx/CloudTrail-Digest/
Whereas I want to list objects from bucket-logs/AWSLogs/accountxxx/CloudTrail/
folder.
Can we do this. Can we filter list object sorted by year/month/date?
Any help ?
EDIT:
I am running the Go programm using the below cli command
./s3_list_objects bucket-logs