Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lakectl upload with pre-sign for Azure #6660

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pkg/api/helpers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"path/filepath"
"strings"

"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
"github.com/treeverse/lakefs/pkg/httputil"
)

// ClientUpload uploads contents as a file via lakeFS
Expand Down Expand Up @@ -102,6 +104,14 @@ func ClientUploadPreSign(ctx context.Context, client apigen.ClientWithResponsesI
}
}

func isAzureBlobURL(u string) bool {
parsedURL, err := url.Parse(u)
if err != nil {
return false
}
return strings.HasSuffix(parsedURL.Host, ".blob.core.windows.net")
}

// clientUploadPreSignHelper helper func to get physical address an upload content. Special case if conflict that
// ErrConflict is returned where a re-try is required.
func clientUploadPreSignHelper(ctx context.Context, client apigen.ClientWithResponsesInterface, repoID string, branchID string, objPath string, metadata map[string]string, contentType string, contents io.ReadSeeker, contentLength int64) (*apigen.ObjectStats, error) {
Expand Down Expand Up @@ -131,13 +141,16 @@ func clientUploadPreSignHelper(ctx context.Context, client apigen.ClientWithResp
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if isAzureBlobURL(preSignURL) {
req.Header.Set("x-ms-blob-type", "BlockBlob")
}

putResp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = putResp.Body.Close() }()
if putResp.StatusCode != http.StatusOK {
if !httputil.IsSuccessStatusCode(putResp) {
return nil, fmt.Errorf("upload %w %s: %s", ErrRequestFailed, preSignURL, putResp.Status)
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/httputil/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package httputil

import "net/http"

// IsSuccessStatusCode returns true for status code 2xx
func IsSuccessStatusCode(response *http.Response) bool {
return response.StatusCode >= http.StatusOK && response.StatusCode < http.StatusMultipleChoices
}
Loading