Last active
January 21, 2022 15:57
-
-
Save DipandaAser/76b38fba49ae724b47d4be7145806edc to your computer and use it in GitHub Desktop.
List all objects of a AWS S3 folder (bypass the limit of 1000 objects)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package storage | |
import ( | |
"context" | |
_ "github.com/aws/aws-sdk-go-v2/feature/s3/manager" | |
"github.com/aws/aws-sdk-go-v2/service/s3" | |
"github.com/aws/aws-sdk-go-v2/service/s3/types" | |
_ "github.com/aws/aws-sdk-go-v2/service/s3/types" | |
) | |
func ListFolderObjects(client *s3.Client, bucket string, prefix string) []types.Object { | |
paginator := s3.NewListObjectsV2Paginator(client, &s3.ListObjectsV2Input{ | |
Bucket: &bucket, | |
Prefix: &prefix, | |
}) | |
objects := []types.Object{} | |
for { | |
if !paginator.HasMorePages() { | |
break | |
} | |
page, err := paginator.NextPage(context.TODO()) | |
if err != nil && page.Contents != nil { | |
objects = append(objects, page.Contents...) | |
} | |
} | |
return objects | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment