Skip to content

Commit

Permalink
fix: downloading and uploading files by netapp-key
Browse files Browse the repository at this point in the history
  • Loading branch information
Artonus committed Mar 5, 2024
1 parent 462ceca commit 43a48af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/data-service/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func (s3Service *S3Service) Fetch(netAppKey, targetDir string) error {

for _, obj := range resp.Contents {
// Specify the local file path to save the object
localFilePath := filepath.Join(targetDir, netAppKey, *obj.Key)
sanitized := strings.Replace(*obj.Key, netAppKey+"/", "", 1)
if sanitized == "" {
continue
}
localFilePath := filepath.Join(targetDir, netAppKey, sanitized)
errCreateDir := util.EnsurePathToFileExists(localFilePath)
if errCreateDir != nil {
return errCreateDir
Expand Down Expand Up @@ -82,7 +86,7 @@ func (s3Service *S3Service) Fetch(netAppKey, targetDir string) error {
func (s3Service *S3Service) Post(netAppKey, sourceDir string) error {
uploader := manager.NewUploader(s3Service.s3)

files, err := os.ReadDir(sourceDir)
files, err := util.ReadAllFiles(filepath.Join(sourceDir, netAppKey))
if err != nil {
return err
}
Expand All @@ -96,7 +100,7 @@ func (s3Service *S3Service) Post(netAppKey, sourceDir string) error {
fmt.Printf("Uploading file: %s\n", file.Name())
_, uploadErr := uploader.Upload(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(s3Service.bucket),
Key: aws.String(file.Name()),
Key: aws.String(filepath.Join(netAppKey, file.Name())),
Body: fileData,
})

Expand Down
18 changes: 18 additions & 0 deletions internal/util/path-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,21 @@ func EnsurePathToFileExists(path string) error {
dir := filepath.Dir(path)
return os.MkdirAll(dir, 0755)
}

// ReadAllFiles lists all files form the directory including subdirectories
func ReadAllFiles(root string) ([]os.DirEntry, error) {
var files []os.DirEntry
err := filepath.WalkDir(root, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, info)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}

0 comments on commit 43a48af

Please sign in to comment.