@@ -14,10 +14,7 @@ import (
1414func UploadDirectoryToSpaces (config config.Config , bucket string , sourcePath string , destPath string ) error {
1515 // Compute the path for the zipped archive of sourcePath
1616 zipFileName := "input.zip"
17- zipFilePath , err := filepath .Abs (filepath .Join (filepath .Dir (sourcePath ), zipFileName ))
18- if err != nil {
19- return err
20- }
17+ zipFilePath := filepath .Join (os .TempDir (), zipFileName )
2118
2219 // Create a file where we will write the zipped archive
2320 fmt .Printf ("Creating zipped archive at %s\n " , zipFilePath )
@@ -40,7 +37,7 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
4037
4138 // If it's a symbolic link, resolve the target
4239 if info .Mode ()& os .ModeSymlink == os .ModeSymlink {
43- target , err : = os .Readlink (path )
40+ target , err = os .Readlink (path )
4441 fmt .Printf ("Resolved link from %s to %s\n " , path , target )
4542 if err != nil {
4643 return err
@@ -57,7 +54,7 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
5754 if targetInfo .IsDir () {
5855 cleanPath := filepath .Clean (path ) + string (filepath .Separator )
5956 fmt .Printf ("Creating directory %s in the zipped archive\n " , cleanPath )
60- _ , err : = zipWriter .Create (cleanPath )
57+ _ , err = zipWriter .Create (cleanPath )
6158 if err != nil {
6259 return err
6360 }
@@ -82,14 +79,19 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
8279 if err != nil {
8380 return err
8481 }
85- defer file .Close ()
8682
8783 // Write this file to the zipped archive
8884 _ , err = io .Copy (zipFileEntry , file )
8985 if err != nil {
9086 return err
9187 }
9288
89+ // Explicitly close the file once we're done to prevent a "too many open files" error
90+ err = file .Close ()
91+ if err != nil {
92+ return err
93+ }
94+
9395 return nil
9496 })
9597 if err != nil {
@@ -98,17 +100,25 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
98100 fmt .Printf ("Successfully added all files from %s to zipped archive at %s\n " , sourcePath , zipFilePath )
99101
100102 // Make sure all prior writes are sync'd to the filesystem
101- // This is necessary because we're going to read the file immediately after writing it
103+ // This is necessary bc we're going to read the file right after writing it
102104 err = zipWriter .Flush ()
103105 if err != nil {
104106 return err
105107 }
106- zipWriter .Close ()
107108 err = zipFile .Sync ()
108109 if err != nil {
109110 return err
110111 }
111- zipFile .Close ()
112+
113+ // Manually Closing is necessary to prevent zip file corruption during upload
114+ err = zipWriter .Close ()
115+ if err != nil {
116+ return err
117+ }
118+ err = zipFile .Close ()
119+ if err != nil {
120+ return err
121+ }
112122
113123 // Read the zipped archive
114124 fileBytes , err := os .ReadFile (zipFilePath )
0 commit comments