Skip to content

Commit

Permalink
func to download bucket from s3
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Pagotto <pagottoo@gmail.com>
  • Loading branch information
pagottoo committed Jul 19, 2022
1 parent 3430cdd commit 9967f33
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -403,3 +404,43 @@ func UploadFile(bucket, key, fileName string) error {
log.Printf("file uploaded to, %s\n", result.Location)
return nil
}

func DownloadBucket(bucket string, destFolder string) error {
// Create a file to write the S3 Object contents to.
// f, err := os.Create(filename)
// if err != nil {
// return fmt.Errorf("failed to create file %q, %v", filename, err)
// }
s3Client := s3.New(GetAWSSession())
downloader := s3manager.NewDownloader(GetAWSSession())

log.Println("Listing the objects in the bucket:")
listObjsResponse, err := s3Client.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(""),
})

if err != nil {
panic("Couldn't list bucket contents")
}

for _, object := range listObjsResponse.Contents {
log.Printf("%s (%d bytes, class %v) \n", *object.Key, object.Size, object.StorageClass)

f, err := pkg.CreateFullPath(filepath.Join(destFolder, *object.Key))
if err != nil {
return fmt.Errorf("failed to create file %q, %v", *object.Key, err)
}

// Write the contents of S3 Object to the file
_, err = downloader.Download(f, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(*object.Key),
})
if err != nil {
return fmt.Errorf("failed to download file, %v", err)
}
f.Close()
}
return nil
}

0 comments on commit 9967f33

Please sign in to comment.