fs

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package fs implements bsw.BlockStorageWrapper for the local file system.

Service issues signed tokens for upload and download. A token is base64url(payload) + "." + base64url(HMAC-SHA256(payload)), where payload holds the operation, bucket, key and expiration time. Tokens can not be forged or changed without the key, and an upload token can not be used for download and vice versa.

FileSystemStorageServer serves the upload and download endpoints that accept these tokens and store objects under a base directory.

On case-insensitive file systems names that differ only in case refer to the same file.

Index

Constants

View Source
const MinKeyLen = 32

MinKeyLen is the minimal length of Config.URLEncryptionKey in bytes.

Variables

View Source
var (
	ErrBasePathNotFound = errors.Template("path not exist").
						Code("BSW-0401").StatusCode(500).Severity(errors.Critical)
	ErrInvalidSignedURL = errors.Template("invalid signed URL").
						Code("BSW-0402").StatusCode(403).Severity(errors.Tiny)
	ErrValidation = errors.Template("validation failed").
					Code("BSW-0403").StatusCode(400).Severity(errors.Tiny)
	ErrSignedURLExpired = errors.Template("signed URL is expired").
						Code("BSW-0404").StatusCode(403).Severity(errors.Tiny)
	ErrObjectNotFound = errors.Template("object not found").
						Code("BSW-0405").StatusCode(404).Severity(errors.Tiny)
	ErrStorage = errors.Template("file storage failure").
				Code("BSW-0406").StatusCode(500).Severity(errors.Medium)
)

Functions

This section is empty.

Types

type AwsCompletedPart deprecated

type AwsCompletedPart = bsw.Part

AwsCompletedPart describes an uploaded part.

Deprecated: use bsw.Part.

type Config

type Config struct {
	// URLEncryptionKey is the secret used to sign tokens with HMAC-SHA256.
	// It must be at least MinKeyLen bytes long. The name is kept for
	// compatibility: tokens are signed, not encrypted.
	URLEncryptionKey string `json:"urlEncryptionKey"`

	// BasePath is the directory objects are stored in. It must exist.
	BasePath string `json:"basePath"`
}

Config holds file storage settings.

type DownloadHandler

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

DownloadHandler handles GET /api/v1/bos/download.

func (*DownloadHandler) Handle

func (c *DownloadHandler) Handle(ctx vatel.Context) error

Handle writes the file to the response.

func (*DownloadHandler) Input

func (c *DownloadHandler) Input() any

Input returns the request parameters.

type FileSystemStorageServer

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

FileSystemStorageServer serves upload and download endpoints and stores objects as files: <basePath>/<bucket>/<key>.

func NewFileSystemStorage

func NewFileSystemStorage(sud SignedURLDecoder, basePath string) *FileSystemStorageServer

NewFileSystemStorage returns a server storing objects under basePath.

func (*FileSystemStorageServer) Endpoints

func (s *FileSystemStorageServer) Endpoints() []vatel.Endpoint

Endpoints returns the upload and download endpoints.

POST /api/v1/bos/upload?dest=<token> expects a multipart form with the file in the "file" field. The request body size is limited by the fasthttp server settings (MaxRequestBodySize).

GET /api/v1/bos/download?src=<token> returns the file.

func (*FileSystemStorageServer) OpenObject added in v0.2.0

func (s *FileSystemStorageServer) OpenObject(o *bsw.Object) (*os.File, int64, error)

OpenObject opens the object file for reading and returns its size. It returns ErrObjectNotFound if the file does not exist.

func (*FileSystemStorageServer) ReadObjectTo

func (s *FileSystemStorageServer) ReadObjectTo(o *bsw.Object, w io.Writer) error

ReadObjectTo copies the object file to w.

func (*FileSystemStorageServer) WriteObject

func (s *FileSystemStorageServer) WriteObject(o *bsw.Object, r io.Reader) error

WriteObject writes r to the object file. Existing content is replaced atomically: data is written to a temporary file which is then renamed.

type Op added in v0.2.0

type Op string

Op is the operation a signed token allows.

const (
	// OpPut allows to upload an object.
	OpPut Op = "put"
	// OpGet allows to download an object.
	OpGet Op = "get"
)

type Service

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

Service signs and verifies upload and download tokens.

func NewFileStorageWrapper added in v0.0.2

func NewFileStorageWrapper(cfg *Config) (*Service, error)

NewFileStorageWrapper returns a service. cfg is copied.

func (*Service) CompleteMultipartUpload

func (s *Service) CompleteMultipartUpload(o *bsw.Object, uploadID string, parts []bsw.CompletedPart) error

CompleteMultipartUpload is not supported.

func (*Service) DecodeSignedURL

func (s *Service) DecodeSignedURL(token string, op Op) (*bsw.Object, error)

DecodeSignedURL verifies the token and returns the object it allows operation op for. It returns ErrInvalidSignedURL if the token is malformed, forged or issued for another operation, and ErrSignedURLExpired if it is expired.

func (*Service) Name added in v0.1.0

func (s *Service) Name() string

Name returns "fs".

func (*Service) PreSignGetObjectURL

func (s *Service) PreSignGetObjectURL(o *bsw.Object, timeout time.Duration) (string, error)

PreSignGetObjectURL returns a signed token for download. The token is passed to the download endpoint in the "src" parameter.

func (*Service) PreSignMultipartObjectURL

func (s *Service) PreSignMultipartObjectURL(o *bsw.Object, timeout time.Duration) ([]string, string, error)

PreSignMultipartObjectURL is not supported.

func (*Service) PreSignPutObjectURL

func (s *Service) PreSignPutObjectURL(o *bsw.Object, timeout time.Duration) (string, error)

PreSignPutObjectURL returns a signed token for upload. The token is passed to the upload endpoint in the "dest" parameter.

type SignedURLDecoder

type SignedURLDecoder interface {
	DecodeSignedURL(token string, op Op) (*bsw.Object, error)
}

SignedURLDecoder verifies tokens. *Service implements it.

type UploadHandler

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

UploadHandler handles POST /api/v1/bos/upload.

It does not implement vatel.Inputer on purpose: vatel decodes the body of a POST request with Input() as JSON, while upload is a multipart form. The token is read with FormValue from the query string or the form.

func (*UploadHandler) Handle

func (c *UploadHandler) Handle(ctx vatel.Context) error

Handle stores the uploaded file.

Jump to

Keyboard shortcuts

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