diff --git a/s3/s3.go b/s3/s3.go index 631ba97325a5f2af7d6bd6fcd77a19105060884b..b036668cb58031e87d1efaeae6a0f071356a4918 100644 --- a/s3/s3.go +++ b/s3/s3.go @@ -538,6 +538,29 @@ func (s ClientWithHelpers) DeleteObjectFromBucket(bucket string, fileName string return nil } +// ListObjectsInBucket - List all objects in an S3 bucket +func (s ClientWithHelpers) ListObjectsInBucket(bucket string) ([]string, error) { + if s.S3Client == nil { + return nil, ErrorS3ClientNotEstablished + } + + // list all the files in the bucket + listObjectsInput := &s3.ListObjectsV2Input{ + Bucket: aws.String(bucket), + } + listObjectsOutput, err := s.S3Client.ListObjectsV2(context.TODO(), listObjectsInput) + if err != nil { + return nil, err + } + + var fileNames []string + for _, object := range listObjectsOutput.Contents { + fileNames = append(fileNames, *object.Key) + } + + return fileNames, nil +} + func GetS3FileKey(fileName string, folder string) string { var fileKey string