Skip to content

Commit

Permalink
fix: don't download when there are no files for the specified Key
Browse files Browse the repository at this point in the history
  • Loading branch information
Artonus committed Mar 15, 2024
1 parent 6df594e commit 40f933e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/data-service/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (s3Service *S3Service) Fetch(netAppKey, targetDir string) error {
// List all objects in the bucket
resp, err := s3Service.s3.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(s3Service.bucket),
Prefix: aws.String(netAppKey),
}, func(o *s3.Options) {
o.Region = s3Service.region
})
Expand All @@ -46,6 +47,15 @@ func (s3Service *S3Service) Fetch(netAppKey, targetDir string) error {
return err
}

if len(resp.Contents) == 0 {
path := filepath.Join(targetDir, netAppKey)
errCreateDir := util.EnsurePathExists(path)
if errCreateDir != nil {
return errCreateDir
}
fmt.Printf("No files to be downloaded")
}

for _, obj := range resp.Contents {
// Specify the local file path to save the object
sanitized := strings.Replace(*obj.Key, netAppKey+"/", "", 1)
Expand Down
4 changes: 4 additions & 0 deletions internal/util/path-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func EnsurePathToFileExists(path string) error {
return os.MkdirAll(dir, 0755)
}

func EnsurePathExists(path string) error {
return os.MkdirAll(path, 0755)
}

// ReadAllFiles lists all files form the directory including subdirectories
func ReadAllFiles(root string) ([]os.DirEntry, error) {
var files []os.DirEntry
Expand Down

0 comments on commit 40f933e

Please sign in to comment.