Skip to content

Commit 170041f

Browse files
Ruben Hönlerubenhoenle
andauthored
fix: use file inputstream on file upload instead of in-memory buffer (#671)
relates to STACKITTPR-70 Co-authored-by: Ruben Hoenle <[email protected]>
1 parent 8409f6b commit 170041f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

stackit/internal/services/iaas/image/resource.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package image
22

33
import (
4-
"bytes"
4+
"bufio"
55
"context"
66
"fmt"
77
"net/http"
@@ -828,16 +828,21 @@ func uploadImage(ctx context.Context, diags *diag.Diagnostics, filePath, uploadU
828828
return fmt.Errorf("upload URL is empty")
829829
}
830830

831-
fileContents, err := os.ReadFile(filePath)
831+
file, err := os.Open(filePath)
832832
if err != nil {
833-
return fmt.Errorf("read file: %w", err)
833+
return fmt.Errorf("open file: %w", err)
834+
}
835+
stat, err := file.Stat()
836+
if err != nil {
837+
return fmt.Errorf("stat file: %w", err)
834838
}
835839

836-
req, err := http.NewRequest(http.MethodPut, uploadURL, bytes.NewReader(fileContents))
840+
req, err := http.NewRequest(http.MethodPut, uploadURL, bufio.NewReader(file))
837841
if err != nil {
838842
return fmt.Errorf("create upload request: %w", err)
839843
}
840844
req.Header.Set("Content-Type", "application/octet-stream")
845+
req.ContentLength = stat.Size()
841846

842847
client := &http.Client{}
843848
resp, err := client.Do(req)

0 commit comments

Comments
 (0)