Moss
Moss

Reputation: 247

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

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.

Upvotes: 18

Views: 20129

Answers (2)

Roshan
Roshan

Reputation: 296

Add Prefix parameter in params

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

will list objects from subfolder/

Upvotes: 28

OhadBasan
OhadBasan

Reputation: 807

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

Upvotes: 3

Related Questions