Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of objects from sub folder of Amazon S3 bucket using golang?

Tags:

go

How do I get the list of objects from a sub folder of an Amazon S3 bucket using golang?

I tried:

svc := s3.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})

params := &s3.ListObjectsInput{
    Bucket: aws.String("bucket"),
}

resp, _ := svc.ListObjects(params)
for _, key := range resp.Contents {
    fmt.Println(*key.Key)
}

I got the list of all the objects in the bucket, but I need only the list of objects in a subfolder.

like image 376
Moss Avatar asked Sep 22 '16 11:09

Moss


1 Answers

Add Prefix parameter in params

params := &s3.ListObjectsInput {
    Bucket: aws.String("bucket"),
    Prefix: aws.String("root/subfolder"),
}

will list objects from subfolder/

like image 50
Roshan Avatar answered Oct 15 '22 01:10

Roshan