Skip to content
Snippets Groups Projects
Commit 1085f68f authored by Daniel Naude's avatar Daniel Naude
Browse files

Add ListObjectsInBucket function to S3 client

The ListObjectsInBucket function allows listing all objects in an S3 bucket. This function retrieves the list of objects using the ListObjectsV2 API and returns an array of file names.
parent f3563244
No related branches found
No related tags found
No related merge requests found
...@@ -538,6 +538,29 @@ func (s ClientWithHelpers) DeleteObjectFromBucket(bucket string, fileName string ...@@ -538,6 +538,29 @@ func (s ClientWithHelpers) DeleteObjectFromBucket(bucket string, fileName string
return nil 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 { func GetS3FileKey(fileName string, folder string) string {
var fileKey string var fileKey string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment