github.com/supabase/cli@v1.168.1/internal/storage/client/api.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/supabase/cli/internal/utils"
     8  	"github.com/supabase/cli/internal/utils/tenant"
     9  	"github.com/supabase/cli/pkg/fetcher"
    10  	"github.com/supabase/cli/pkg/storage"
    11  )
    12  
    13  func NewStorageAPI(ctx context.Context, projectRef string) (storage.StorageAPI, error) {
    14  	server := fmt.Sprintf("http://%s:%d", utils.Config.Hostname, utils.Config.Api.Port)
    15  	token := utils.Config.Auth.ServiceRoleKey
    16  	if len(projectRef) > 0 {
    17  		server = "https://" + utils.GetSupabaseHost(projectRef)
    18  		apiKey, err := tenant.GetApiKeys(ctx, projectRef)
    19  		if err != nil {
    20  			return storage.StorageAPI{}, err
    21  		}
    22  		token = apiKey.ServiceRole
    23  	}
    24  	api := storage.StorageAPI{Fetcher: fetcher.NewFetcher(
    25  		server,
    26  		fetcher.WithBearerToken(token),
    27  		fetcher.WithUserAgent("SupabaseCLI/"+utils.Version),
    28  	)}
    29  	return api, nil
    30  }