cystore

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidConfig     = errors.New("invalid storage configuration")
	ErrProviderNotFound  = errors.New("storage provider not found")
	ErrBucketNotFound    = errors.New("bucket not found")
	ErrObjectNotFound    = errors.New("object not found")
	ErrInvalidObjectName = errors.New("invalid object name")
)

Common errors

View Source
var (
	ErrBucketRequired = errors.New("bucket is required and no default bucket is set")
)

Functions

func BuildPath

func BuildPath(components ...string) string

BuildPath builds a path from components

func GetContentType

func GetContentType(filename string) string

GetContentType determines the content type based on file extension

func RegistProvider

func RegistProvider(name string, funcProvider FuncNewStore)

func SplitPath

func SplitPath(path string) (dir, file string)

SplitPath splits a path into directory and filename

Types

type BucketInfo

type BucketInfo struct {
	Name         string    // Name of the bucket
	CreationDate time.Time // Creation date of the bucket
}

BucketInfo contains information about a bucket

type Config

type Config struct {
	// Provider type (minio, local, etc.)
	Provider ProviderType `json:"provider" yaml:"provider"`

	// Common settings
	Region  string        `json:"region" yaml:"region"`
	Secure  bool          `json:"secure" yaml:"secure"`
	Timeout time.Duration `json:"timeout" yaml:"timeout"`

	// Unified storage settings
	// These fields are used across different providers
	Endpoint     string `json:"endpoint" yaml:"endpoint"`
	AccessKey    string `json:"access_key" yaml:"access_key"`
	SecretKey    string `json:"secret_key" yaml:"secret_key"`
	SessionToken string `json:"session_token,omitempty" yaml:"session_token,omitempty"`
	UseSSL       bool   `json:"use_ssl" yaml:"use_ssl"`

	// Provider-specific settings that don't fit in the common fields
	// Local storage specific
	BasePath string `json:"base_path,omitempty" yaml:"base_path,omitempty"`

	// SFTP specific settings
	SSHPort        int    `json:"ssh_port,omitempty" yaml:"ssh_port,omitempty"`                 // SSH port (default: 22)
	SSHUser        string `json:"ssh_user,omitempty" yaml:"ssh_user,omitempty"`                 // SSH username
	SSHPassword    string `json:"ssh_password,omitempty" yaml:"ssh_password,omitempty"`         // SSH password
	SSHPrivateKey  string `json:"ssh_private_key,omitempty" yaml:"ssh_private_key,omitempty"`   // SSH private key path or content
	SSHKeyPassword string `json:"ssh_key_password,omitempty" yaml:"ssh_key_password,omitempty"` // SSH private key password
}

Config represents the unified configuration for all storage providers

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration

type FuncNewStore

type FuncNewStore func(config *Config) (Provider, error)

type GetObjectOptions

type GetObjectOptions struct {
	Range        string // Range of bytes to download
	MatchETag    string // Download object if ETag matches
	NotMatchETag string // Download object if ETag doesn't match
}

GetObjectOptions specifies options for GetObject operation

type ObjectInfo

type ObjectInfo struct {
	Bucket       string            // Bucket name
	Name         string            // Object name
	ETag         string            // ETag of the object
	Size         int64             // Size of the object
	LastModified time.Time         // Last modified time of the object
	ContentType  string            // Content type of the object
	Metadata     map[string]string // User-defined metadata
}

ObjectInfo contains information about an object

type Option

type Option func(*Store)

func WithBucket

func WithBucket(bucket string, ensureExists ...bool) Option

WithBucket sets the default bucket for all operations If ensureExists is true, it will ensure the bucket exists when the store is created

type Provider

type Provider interface {
	// Bucket operations
	BucketExists(ctx context.Context, bucketName string) (bool, error)
	CreateBucket(ctx context.Context, bucketName string) error
	RemoveBucket(ctx context.Context, bucketName string) error
	ListBuckets(ctx context.Context) ([]BucketInfo, error)

	// Object operations
	PutObject(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts PutObjectOptions) (ObjectInfo, error)
	GetObject(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (io.ReadCloser, ObjectInfo, error)
	StatObject(ctx context.Context, bucketName, objectName string) (ObjectInfo, error)
	RemoveObject(ctx context.Context, bucketName, objectName string) error
	ListObjects(ctx context.Context, bucketName, prefix string, recursive bool) <-chan ObjectInfo

	// Batch operations
	RemoveObjects(ctx context.Context, bucketName string, objectNames []string) <-chan RemoveObjectError

	// Copy/Rename operations
	CopyObject(ctx context.Context, srcBucket, srcObject, dstBucket, dstObject string) (ObjectInfo, error)

	// Presigned URL operations
	PresignedGetObject(ctx context.Context, bucketName, objectName string, expires time.Duration) (string, error)
	PresignedPutObject(ctx context.Context, bucketName, objectName string, expires time.Duration) (string, error)
}

Provider defines the interface for cloud storage operations

type ProviderType

type ProviderType string

ProviderType represents the type of storage provider

const (
	// ProviderMinio represents MinIO/S3 compatible storage
	ProviderMinio ProviderType = "minio"
	// ProviderLocal represents local file system storage
	ProviderLocal ProviderType = "local"
	// ProviderHuaweiOBS represents Huawei Cloud Object Storage Service
	ProviderHuaweiOBS ProviderType = "huawei_obs"
	// ProviderAliyunOSS represents Alibaba Cloud Object Storage Service
	ProviderAliyunOSS ProviderType = "aliyun_oss"
	// ProviderNFS represents NFS (Network File System) storage
	ProviderNFS ProviderType = "nfs"
	// ProviderSFTP represents SFTP (SSH File Transfer Protocol) storage
	ProviderSFTP ProviderType = "sftp"
)

type PutObjectOptions

type PutObjectOptions struct {
	ContentType string            // Content type of the object
	Metadata    map[string]string // User-defined metadata
}

PutObjectOptions specifies options for PutObject operation

type RemoveObjectError added in v1.0.0

type RemoveObjectError struct {
	ObjectName string // Name of the object
	Error      error  // Error that occurred during removal
}

RemoveObjectError contains information about a failed object removal

type Store

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

Store provides a unified interface for cloud storage operations

func NewStore

func NewStore(config *Config, opts ...Option) (*Store, error)

NewStore creates a new storage client based on the provided configuration

func (*Store) Config

func (s *Store) Config() *Config

Config returns the store configuration

func (*Store) CopyObject added in v1.0.0

func (s *Store) CopyObject(ctx context.Context, srcObject string, dstObject string, srcBucket ...string) error

CopyObject copies an object from source to destination If srcBucket or dstBucket is empty, the default bucket will be used

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, objectPath string, bucket ...string) error

Delete deletes a file from the specified bucket and object path If bucket is nil, the default bucket will be used if set

func (*Store) DeleteBatch added in v1.0.0

func (s *Store) DeleteBatch(ctx context.Context, objectPaths []string, bucket ...string) (<-chan RemoveObjectError, error)

DeleteBatch deletes multiple files from the specified bucket Returns a channel of RemoveObjectError for any failures If bucket is nil, the default bucket will be used if set

func (*Store) Download

func (s *Store) Download(ctx context.Context, objectPath string, bucket ...string) (io.ReadCloser, error)

Download downloads a file from the specified bucket and object path If bucket is nil, the default bucket will be used if set

func (*Store) EnsureBucket

func (s *Store) EnsureBucket(ctx context.Context, bucketName string) error

EnsureBucket ensures that a bucket exists, creating it if necessary

func (*Store) FileExists

func (s *Store) FileExists(ctx context.Context, objectPath string, bucket ...string) (bool, error)

FileExists checks if a file exists

func (*Store) GeneratePresignedURL

func (s *Store) GeneratePresignedURL(ctx context.Context, objectPath string, expiry time.Duration, bucket ...string) (string, error)

GeneratePresignedURL generates a presigned URL for the given object If bucket is nil, the default bucket will be used if set

func (*Store) GetObjectInfo

func (s *Store) GetObjectInfo(ctx context.Context, objectPath string, bucket ...string) (ObjectInfo, error)

GetObjectInfo gets metadata for an object If bucket is nil, the default bucket will be used if set

func (*Store) ListObjects

func (s *Store) ListObjects(ctx context.Context, prefix string, bucket ...string) ([]ObjectInfo, error)

ListObjects lists objects in a bucket with the given prefix (non-recursive) If bucket is nil, the default bucket will be used if set

func (*Store) ListObjectsChan added in v1.0.0

func (s *Store) ListObjectsChan(ctx context.Context, prefix string, recursive bool, bucket ...string) (<-chan ObjectInfo, error)

ListObjectsChan returns a channel of objects for streaming iteration If bucket is nil, the default bucket will be used if set

func (*Store) ListObjectsRecursive added in v1.0.0

func (s *Store) ListObjectsRecursive(ctx context.Context, prefix string, bucket ...string) ([]ObjectInfo, error)

ListObjectsRecursive lists objects in a bucket with the given prefix recursively If bucket is nil, the default bucket will be used if set

func (*Store) Provider

func (s *Store) Provider() Provider

Provider returns the underlying provider

func (*Store) PutURL

func (s *Store) PutURL(ctx context.Context, bucketName, objectPath string, expirySeconds int) (string, error)

PutURL generates a presigned URL for uploading an object

func (*Store) RenameObject added in v1.0.0

func (s *Store) RenameObject(ctx context.Context, oldName, newName string, bucket ...string) error

RenameObject renames an object within the same bucket If bucket is nil, the default bucket will be used if set

func (*Store) Upload

func (s *Store) Upload(ctx context.Context, objectPath string, data io.Reader, size int64, contentType string, bucket ...string) (string, error)

Upload uploads a file to the specified bucket and object path If bucket is nil, the default bucket will be used if set

func (*Store) WalkObjects added in v1.0.0

func (s *Store) WalkObjects(ctx context.Context, prefix string, fn func(ObjectInfo) error, bucket ...string) error

WalkObjects iterates through objects with the given prefix and calls fn for each object If bucket is nil, the default bucket will be used if set

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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