fix: use file inputstream on file upload instead of in-memory buffer (#671)
relates to STACKITTPR-70 Co-authored-by: Ruben Hoenle <Ruben.Hoenle@stackit.cloud>
This commit is contained in:
parent
8409f6b590
commit
170041f807
1 changed files with 9 additions and 4 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
package image
|
package image
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -828,16 +828,21 @@ func uploadImage(ctx context.Context, diags *diag.Diagnostics, filePath, uploadU
|
||||||
return fmt.Errorf("upload URL is empty")
|
return fmt.Errorf("upload URL is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
fileContents, err := os.ReadFile(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("read file: %w", err)
|
return fmt.Errorf("open file: %w", err)
|
||||||
|
}
|
||||||
|
stat, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("stat file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPut, uploadURL, bytes.NewReader(fileContents))
|
req, err := http.NewRequest(http.MethodPut, uploadURL, bufio.NewReader(file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create upload request: %w", err)
|
return fmt.Errorf("create upload request: %w", err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
req.ContentLength = stat.Size()
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue