55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func (cli *Client) PutUpload(ctx context.Context, rawref string, src io.Reader, uploc, digest string, size int64) (string, error) {
|
|
var err error
|
|
var bloc string
|
|
|
|
ref, err := NewReference(rawref)
|
|
if err != nil {
|
|
return bloc, err
|
|
}
|
|
uri, err := ref.Put(uploc, digest)
|
|
if err != nil {
|
|
return bloc, err
|
|
}
|
|
user, pass := ref.Userinfo()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, src)
|
|
if err != nil {
|
|
return bloc, err
|
|
}
|
|
req.Header.Set("User-Agent", cli.userAgent)
|
|
req.Header.Set("Content-Type", "application/octet-stream")
|
|
req.Header.Set("Content-Length", strconv.FormatInt(size, 10))
|
|
if cli.authenticator != nil {
|
|
authHeader, authKey, err := cli.authenticator.MakeHeader(user, pass)
|
|
if err != nil {
|
|
return bloc, err
|
|
}
|
|
req.Header.Set(authHeader, authKey)
|
|
}
|
|
resp, err := cli.httpClient.Do(req)
|
|
if err != nil {
|
|
return bloc, err
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusCreated {
|
|
err = fmt.Errorf("Upload not accepted, code %d", resp.StatusCode)
|
|
return bloc, err
|
|
}
|
|
bloc = resp.Header.Get("Location")
|
|
if bloc == "" {
|
|
err := fmt.Errorf("Empty blob location declaration")
|
|
return bloc, err
|
|
}
|
|
return bloc, err
|
|
}
|