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.
Add Prefix parameter in params
params := &s3.ListObjectsInput {
Bucket: aws.String("bucket"),
Prefix: aws.String("root/subfolder"),
}
will list objects from subfolder/
in order to access the folders (they are not really folders, as s3 is an object storage) you have to provide the Prefix and Delimiter attributes to ListObjectsInput say that you have s3://foo/bar you can provide the "foo/bar" prefix with the '/' delimiter to get all the subobjects