storage

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SchemeGCS is the canonical Google Cloud Storage URI scheme used by
	// gsutil and emitted by every URL we generate (StorageURL, exports, etc.).
	SchemeGCS = "gs://"
	// SchemeGCSAlias is accepted on input as a synonym for SchemeGCS so users
	// who type "gcs://" (the service abbreviation) get the same behavior.
	// Output is always normalized to SchemeGCS.
	SchemeGCSAlias    = "gcs://"
	PathNativeScans   = "native-scans"
	PathAgenticScans  = "agentic-scans"
	PathUGC           = "ugc"
	ResultsBundleName = "results.tar.gz"
)

Variables

View Source
var ErrObjectNotFound = errors.New("object not found")

ErrObjectNotFound is returned by Stat when the object is absent. Callers distinguish it from a transport/credential failure so a genuine outage isn't reported to the client as a 404.

View Source
var ErrPathTraversal = fmt.Errorf("path contains traversal or invalid characters")

Functions

func AgenticScanResultKey

func AgenticScanResultKey(agenticScanUUID string) string

AgenticScanResultKey returns the storage key for an agentic scan result bundle.

func IsGCSURI

func IsGCSURI(s string) bool

IsGCSURI reports whether s starts with the canonical SchemeGCS prefix or the SchemeGCSAlias synonym. Use this instead of strings.HasPrefix(s, "gs://") at any user-input boundary so "gcs://" works interchangeably.

func NativeScanResultKey

func NativeScanResultKey(scanUUID string) string

NativeScanResultKey returns the storage key for a native scan result bundle.

func NormalizeGCSURI

func NormalizeGCSURI(s string) string

NormalizeGCSURI rewrites a "gcs://" prefix to "gs://" so downstream code can keep matching a single canonical form. URIs that already use the canonical prefix (or neither) are returned unchanged.

func ParseGCSPath

func ParseGCSPath(uri string) (projectUUID, key string, err error)

ParseGCSPath parses a gs://<project-uuid>/<key> URI into project UUID and key. The "gcs://" alias is accepted and normalized to the canonical "gs://" form before parsing. Returns empty strings if the URI doesn't match the expected format.

func ResolveGCSSource

func ResolveGCSSource(storageCfg *config.StorageConfig, sourcePath, projectUUID string) (extractedPath string, cleanup func(), err error)

ResolveGCSSource downloads and extracts a gs://<project>/<key> archive into a temp directory. The returned cleanup must be invoked once the run is done to remove the temp tree. When projectUUID is empty, it falls back to the project component parsed from the URI.

func StorageURL

func StorageURL(projectUUID, key string) string

StorageURL builds a gs:// URL from project UUID and key.

func StripBucketPrefix

func StripBucketPrefix(uri, bucket string) string

StripBucketPrefix collapses gsutil-style "gs://<bucket>/<rest>" URIs to the project-relative "gs://<rest>" form expected by ParseGCSPath. The console's upload path emits gs://<bucket>/<projectUUID>/<key> (lib/storage-shared.ts), while ParseGCSPath expects gs://<projectUUID>/<key>; without this collapse the two layers disagree on whether the first segment is the bucket or the project. URIs whose first segment doesn't match bucket — and non-gs:// URIs — are returned unchanged.

func UGCKey

func UGCKey(filename string) string

UGCKey returns the storage key for a user-uploaded file.

func ValidateKey

func ValidateKey(key string) (string, error)

ValidateKey rejects keys that attempt path traversal or project scope escape. Returns the cleaned key or an error.

func ValidateProjectUUID

func ValidateProjectUUID(projectUUID string) error

ValidateProjectUUID rejects project UUIDs that contain path separators or traversal.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps minio-go to provide project-scoped object storage operations.

func NewClient

func NewClient(cfg *config.StorageConfig) (*Client, error)

NewClient creates a storage client from the StorageConfig.

func (*Client) BundleAndUploadFiles

func (c *Client) BundleAndUploadFiles(ctx context.Context, projectUUID, key string, filePaths map[string]string) (string, error)

BundleAndUploadFiles creates a tar.gz of specific files and uploads it. filePaths is a map of arcName → localPath.

func (*Client) BundleAndUploadResults

func (c *Client) BundleAndUploadResults(ctx context.Context, projectUUID, key, sourceDir string) (string, error)

BundleAndUploadResults creates a tar.gz of the given directory and uploads it.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, projectUUID, key string) error

func (*Client) Download

func (c *Client) Download(ctx context.Context, projectUUID, key string) (io.ReadCloser, error)

Download returns a reader for the specified object. Caller must close the returned reader.

func (*Client) DownloadAndExtractSource

func (c *Client) DownloadAndExtractSource(ctx context.Context, projectUUID, key string) (string, string, error)

DownloadAndExtractSource downloads an archive from storage and extracts it to a temp directory. The format is dispatched off the key's extension to match the formats the console upload route accepts (zip, tar.gz/tgz, tar). Returns the path to the extracted source and the temp root directory. Callers should defer os.RemoveAll(tmpRoot) to clean up.

func (*Client) DownloadToFile

func (c *Client) DownloadToFile(ctx context.Context, projectUUID, key, destPath string) error

func (*Client) List

func (c *Client) List(ctx context.Context, projectUUID, prefix string) ([]ObjectInfo, error)

func (*Client) PresignedGetURL

func (c *Client) PresignedGetURL(ctx context.Context, projectUUID, key string, expiry time.Duration) (string, error)

func (*Client) PresignedPutURL

func (c *Client) PresignedPutURL(ctx context.Context, projectUUID, key string, expiry time.Duration) (string, error)

func (*Client) Stat

func (c *Client) Stat(ctx context.Context, projectUUID, key string) (ObjectInfo, error)

Stat returns metadata for a single object without transferring its body.

It doubles as the existence probe: minio's GetObject is lazy and only fails on first Read, so a caller that needs to fall back to another key on a miss — or to answer 404 before committing response headers — must ask first, and Stat's size is what a streaming download needs for Content-Length. One StatObject round trip serves both.

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, projectUUID, key string, reader io.Reader, size int64, contentType string) error

func (*Client) UploadFile

func (c *Client) UploadFile(ctx context.Context, projectUUID, key, filePath string) error

type ObjectInfo

type ObjectInfo struct {
	Key          string    `json:"key"`
	Size         int64     `json:"size"`
	LastModified time.Time `json:"last_modified"`
	ContentType  string    `json:"content_type"`
}

ObjectInfo represents metadata for a listed object.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL