Skip to content

Commit

Permalink
fix: upload directory
Browse files Browse the repository at this point in the history
  • Loading branch information
haristhohir committed Dec 9, 2024
1 parent af086b5 commit 9683f42
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gcs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,20 @@ func (g gcsManager) UploadDirectory(bucketName, localDir, gcsPrefix string) erro
return nil
}

gcsPath := strings.TrimPrefix(localPath, localDir)
gcsObject := gcsPrefix + gcsPath
localPath = strings.ReplaceAll(localPath, "\\", "/")
localDir = strings.ReplaceAll(localDir, "\\", "/")

relativePath := strings.TrimPrefix(localPath, strings.Replace(localDir, "./", "", 1))
relativePath = strings.TrimPrefix(relativePath, string(filepath.Separator))
gcsPath := filepath.Join(gcsPrefix, relativePath)

file, err := os.Open(localPath)
if err != nil {
return fmt.Errorf("os.Open %v: %v", localPath, err)
}
defer file.Close()

writer := g.client.Bucket(bucketName).Object(gcsObject).NewWriter(g.ctx)
writer := g.client.Bucket(bucketName).Object(gcsPath).NewWriter(g.ctx)
if _, err := io.Copy(writer, file); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
Expand All @@ -176,7 +180,7 @@ func (g gcsManager) UploadDirectory(bucketName, localDir, gcsPrefix string) erro
return fmt.Errorf("writer.Close: %v", err)
}

fmt.Printf("Uploaded %s to gs://%s/%s\n", localPath, bucketName, gcsObject)
fmt.Printf("Uploaded %s to gs://%s/%s\n", localPath, bucketName, gcsPath)
return nil
})

Expand Down

0 comments on commit 9683f42

Please sign in to comment.