Skip to content

Commit

Permalink
chore: use upsert header when overwriting object
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Aug 8, 2024
1 parent 4b0083e commit 61c610c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/seed/buckets/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func Run(ctx context.Context, projectRef string, interactive bool, fsys afero.Fs
}
resolved[name] = bucket
}
return api.UpsertObjects(ctx, resolved, 5, utils.NewRootFS(fsys))
return api.UpsertObjects(ctx, resolved, utils.NewRootFS(fsys))
}
21 changes: 12 additions & 9 deletions pkg/storage/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/go-errors/errors"
"github.com/supabase/cli/pkg/config"
Expand Down Expand Up @@ -57,8 +56,17 @@ func (s *StorageAPI) UpsertBuckets(ctx context.Context, bucketConfig config.Buck
return nil
}

func (s *StorageAPI) UpsertObjects(ctx context.Context, bucketConfig config.BucketConfig, maxJobs uint, fsys fs.FS, opts ...func(*FileOptions)) error {
jq := queue.NewJobQueue(maxJobs)
type UploadOptions struct {
MaxConcurrency uint
KeyPrefix string
}

func (s *StorageAPI) UpsertObjects(ctx context.Context, bucketConfig config.BucketConfig, fsys fs.FS, opts ...func(*UploadOptions)) error {
uo := UploadOptions{MaxConcurrency: 5}
for _, apply := range opts {
apply(&uo)
}
jq := queue.NewJobQueue(uo.MaxConcurrency)
for name, bucket := range bucketConfig {
localPath := bucket.ObjectsPath
if len(localPath) == 0 {
Expand All @@ -71,7 +79,7 @@ func (s *StorageAPI) UpsertObjects(ctx context.Context, bucketConfig config.Buck
if !info.Type().IsRegular() {
return nil
}
var dstPath string
dstPath := uo.KeyPrefix
relPath, err := filepath.Rel(localPath, filePath)
if err != nil {
return errors.Errorf("failed to resolve relative path: %w", err)
Expand All @@ -92,11 +100,6 @@ func (s *StorageAPI) UpsertObjects(ctx context.Context, bucketConfig config.Buck
if err != nil {
return err
}
if err := s.UploadObjectStream(ctx, dstPath, f, *fo); err == nil {
return nil
} else if !strings.Contains(err.Error(), `"statusCode":"409"`) {
return err
}
fo.Overwrite = true
return s.UploadObjectStream(ctx, dstPath, f, *fo)
}
Expand Down
17 changes: 10 additions & 7 deletions pkg/storage/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,19 @@ func (s *StorageAPI) UploadObject(ctx context.Context, remotePath, localPath str

func (s *StorageAPI) UploadObjectStream(ctx context.Context, remotePath string, localFile io.Reader, fo FileOptions) error {
headers := func(req *http.Request) {
req.Header.Add("Content-Type", fo.ContentType)
req.Header.Add("Cache-Control", fo.CacheControl)
if len(fo.ContentType) > 0 {
req.Header.Add("Content-Type", fo.ContentType)
}
if len(fo.CacheControl) > 0 {
req.Header.Add("Cache-Control", fo.CacheControl)
}
if fo.Overwrite {
req.Header.Add("x-upsert", "true")
}
}
// Prepare request
remotePath = strings.TrimPrefix(remotePath, "/")
method := http.MethodPost
if fo.Overwrite {
method = http.MethodPut
}
resp, err := s.Send(ctx, method, "/storage/v1/object/"+remotePath, io.NopCloser(localFile), headers)
resp, err := s.Send(ctx, http.MethodPost, "/storage/v1/object/"+remotePath, io.NopCloser(localFile), headers)
if err != nil {
return err
}
Expand Down

0 comments on commit 61c610c

Please sign in to comment.