Download files in parallel

This commit is contained in:
M. Mert Yildiran 2023-06-29 16:45:59 +03:00
parent 98738cb5a6
commit aeda619104
No known key found for this signature in database
GPG Key ID: DA5D6DCBB758A461

View File

@ -12,11 +12,13 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config" awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager" "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"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -330,7 +332,11 @@ func downloadTarFromS3(s3Url string) (tarPath string, err error) {
return return
} }
var wg sync.WaitGroup
for _, object := range listObjectsOutput.Contents { for _, object := range listObjectsOutput.Contents {
wg.Add(1)
go func(object s3Types.Object) {
defer wg.Done()
objectKey := *object.Key objectKey := *object.Key
fullPath := filepath.Join(tempDirPath, objectKey) fullPath := filepath.Join(tempDirPath, objectKey)
@ -356,7 +362,9 @@ func downloadTarFromS3(s3Url string) (tarPath string, err error) {
if err != nil { if err != nil {
return return
} }
}(object)
} }
wg.Wait()
tarPath, err = tarDirectory(tempDirPath) tarPath, err = tarDirectory(tempDirPath)
return return