storage

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

README

storage

File storage abstraction with presigned URL support. Backends are pluggable — swap between local filesystem (development) and AWS S3 (production) without changing application code.

Usage

import (
    "github.com/OpenNSW/core/storage"
)

driver, err := storage.NewStorageFromConfig(ctx, storage.Config{
    Type: "s3",
    Options: map[string]string{
        "bucket": "my-uploads",
        "region": "ap-southeast-2",
    },
})
svc := storage.NewService(driver)

Use "local" as Type for development (stores files under Options["base_dir"]).

Operations

Upload (presigned)
meta, err := svc.Upload(ctx, "passport.pdf", fileSize, "application/pdf")
// meta.Key       — opaque storage key; persist this to your database
// meta.UploadURL — presigned PUT URL; return to the client for direct upload

The client uploads directly to the storage backend — the file never passes through your application server.

Download
// Stream the file contents
content, mimeType, err := svc.Download(ctx, fileKey)

// Or get a presigned download URL for the client
meta, err := svc.GetDownloadURL(ctx, fileKey)
// meta.DownloadURL — presigned GET URL valid for a short window
Delete
err := svc.Delete(ctx, fileKey)

Implementing a custom driver

type StorageDriver interface {
    Save(ctx context.Context, key string, r io.Reader, size int64, mimeType string) error
    Get(ctx context.Context, key string) (io.ReadCloser, string, error)
    Delete(ctx context.Context, key string) error
    GetDownloadURL(ctx context.Context, key string) (string, error)
    GetUploadURL(ctx context.Context, key, mimeType string, size int64) (string, error)
}

Register your driver by passing it directly to storage.NewService(driver).

Config reference

S3
Option Description
bucket S3 bucket name
region AWS region (e.g. ap-southeast-2)
endpoint Custom endpoint URL (for MinIO or localstack)
access_key_id AWS access key (falls back to environment / instance profile)
secret_access_key AWS secret key
Local filesystem
Option Description
base_dir Directory to store files under (created if absent)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Type           string // "local" or "s3"
	LocalBaseDir   string
	LocalPublicURL string
	S3Endpoint     string
	S3Bucket       string
	S3Region       string
	S3AccessKey    string
	S3SecretKey    string
	S3UseSSL       bool
	S3PublicURL    string
	LocalPutSecret string
	PresignTTL     time.Duration
}

func (Config) Validate

func (c Config) Validate() error

type FileMetadata

type FileMetadata struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Key       string `json:"key"`
	URL       string `json:"url,omitempty"`
	UploadURL string `json:"upload_url,omitempty"`
	Size      int64  `json:"size"`
	MimeType  string `json:"mime_type"`
}

FileMetadata represents the metadata of an uploaded file

type HTTPHandler

type HTTPHandler struct {
	Service *Service
}

func NewHTTPHandler

func NewHTTPHandler(service *Service) *HTTPHandler

func (*HTTPHandler) Delete

func (h *HTTPHandler) Delete(w http.ResponseWriter, r *http.Request)

func (*HTTPHandler) Download

func (h *HTTPHandler) Download(w http.ResponseWriter, r *http.Request)

func (*HTTPHandler) DownloadContent

func (h *HTTPHandler) DownloadContent(w http.ResponseWriter, r *http.Request)

DownloadContent streams the file body directly from the local filesystem driver. It is intended only for local development when using LocalFSDriver; in non-local environments (e.g. S3) callers should use GetDownloadURL and presigned URLs instead.

func (*HTTPHandler) Upload

func (h *HTTPHandler) Upload(w http.ResponseWriter, r *http.Request)

func (*HTTPHandler) UploadContentLocal

func (h *HTTPHandler) UploadContentLocal(w http.ResponseWriter, r *http.Request)

UploadContentLocal acts as a mock S3 bucket for local development. It accepts a PUT request with the raw file body.

type Service

type Service struct {
	Driver StorageDriver
}

Service coordinates file storage operations and manages metadata

func NewService

func NewService(driver StorageDriver) *Service

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, key string) error

Delete removes a file from storage

func (*Service) Download

func (s *Service) Download(ctx context.Context, key string) (io.ReadCloser, string, error)

Download retrieves the file content and its MIME type

func (*Service) GetDownloadURL

func (s *Service) GetDownloadURL(ctx context.Context, key string) (string, error)

GetDownloadURL generates a time-limited or presigned URL for the given key

func (*Service) Upload

func (s *Service) Upload(ctx context.Context, filename string, size int64, mime string) (*FileMetadata, error)

Upload handles the preparation of a file upload by generating a unique key and a presigned/upload URL via the storage driver.

type StorageDriver

type StorageDriver interface {
	// Save writes the content to the storage and returns a unique identifier (key/path)
	Save(ctx context.Context, key string, body io.Reader, contentType string) error

	// Get returns a ReadCloser to stream the file back and its content type
	Get(ctx context.Context, key string) (io.ReadCloser, string, error)

	// Delete removes the file
	Delete(ctx context.Context, key string) error

	// GetDownloadURL returns a presigned or time-limited URL for downloading
	GetDownloadURL(ctx context.Context, key string) (string, error)

	// GetUploadURL returns a presigned URL for uploading a file directly to storage
	GetUploadURL(ctx context.Context, key string, contentType string, maxSizeBytes int64) (string, error)
}

StorageDriver defines how we interact with the binary storage

func NewStorageFromConfig

func NewStorageFromConfig(ctx context.Context, cfg Config) (StorageDriver, error)

NewStorageFromConfig creates a storage instance based on the provided configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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