client

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package client provides an S3 client wrapper with configuration management.

Index

Constants

View Source
const (
	DefaultRegion  = "us-east-1"
	DefaultTimeout = 30 * time.Second
)

Default values for configuration.

Variables

This section is empty.

Functions

func SanitizeAWSEnvVars added in v0.1.3

func SanitizeAWSEnvVars()

SanitizeAWSEnvVars clears AWS environment variables that contain unresolved template variables. This prevents the AWS SDK from using invalid values when mcpb passes through unresolved templates. This function must be called before config.LoadDefaultConfig() as the AWS SDK reads these environment variables directly.

Types

type BucketInfo

type BucketInfo struct {
	Name         string
	CreationDate time.Time
}

BucketInfo contains information about an S3 bucket.

type Client

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

Client wraps the AWS S3 SDK client with convenience methods.

func New

func New(ctx context.Context, cfg *Config) (*Client, error)

New creates a new S3 client with the given configuration.

func (*Client) Close

func (c *Client) Close() error

Close closes the S3 client and releases resources. Currently a no-op as the AWS SDK manages its own connection pool.

func (*Client) Config

func (c *Client) Config() *Config

Config returns a copy of the client configuration.

func (*Client) ConnectionName

func (c *Client) ConnectionName() string

ConnectionName returns the configured connection name.

func (*Client) CopyObject

func (c *Client) CopyObject(ctx context.Context, input *CopyObjectInput) (*CopyObjectOutput, error)

CopyObject copies an object within or between buckets.

func (*Client) DeleteObject

func (c *Client) DeleteObject(ctx context.Context, bucket, key string) error

DeleteObject deletes an object from S3.

func (*Client) GetObject

func (c *Client) GetObject(ctx context.Context, bucket, key string) (*ObjectContent, error)

GetObject retrieves an object's content from S3.

func (*Client) GetObjectMetadata

func (c *Client) GetObjectMetadata(ctx context.Context, bucket, key string) (*ObjectMetadata, error)

GetObjectMetadata retrieves an object's metadata without downloading the content.

func (*Client) ListBuckets

func (c *Client) ListBuckets(ctx context.Context) ([]BucketInfo, error)

ListBuckets returns a list of all buckets accessible to the client.

func (*Client) ListObjects

func (c *Client) ListObjects(
	ctx context.Context, bucket, prefix, delimiter string, maxKeys int32, continueToken string,
) (*ListObjectsOutput, error)

ListObjects lists objects in a bucket with optional prefix, delimiter, and pagination.

func (*Client) PresignGetURL

func (c *Client) PresignGetURL(ctx context.Context, bucket, key string, expires time.Duration) (*PresignedURL, error)

PresignGetURL generates a presigned URL for downloading an object.

func (*Client) PresignPutURL

func (c *Client) PresignPutURL(ctx context.Context, bucket, key string, expires time.Duration) (*PresignedURL, error)

PresignPutURL generates a presigned URL for uploading an object.

func (*Client) PutObject

func (c *Client) PutObject(ctx context.Context, input *PutObjectInput) (*PutObjectOutput, error)

PutObject uploads an object to S3.

type Config

type Config struct {
	// Region is the AWS region for the S3 service.
	Region string

	// Endpoint is an optional custom endpoint URL for S3-compatible services (SeaweedFS, LocalStack, etc.).
	Endpoint string

	// AccessKeyID is the AWS access key ID. If empty, the SDK credential chain is used.
	AccessKeyID string

	// SecretAccessKey is the AWS secret access key.
	SecretAccessKey string

	// SessionToken is an optional session token for temporary credentials.
	SessionToken string

	// Profile is an optional AWS profile name to use from shared credentials/config.
	Profile string

	// UsePathStyle enables path-style addressing instead of virtual-hosted style.
	// Required for some S3-compatible services like SeaweedFS.
	UsePathStyle bool

	// Timeout is the timeout for S3 operations.
	Timeout time.Duration

	// Name is an optional identifier for this connection (used in multi-connection setups).
	Name string

	// DisableSSL disables SSL/TLS for the connection (useful for local development).
	DisableSSL bool
}

Config holds the configuration for connecting to an S3-compatible storage service.

func FromEnv

func FromEnv() Config

FromEnv creates a Config populated from environment variables.

Environment variables:

  • AWS_REGION: AWS region (default: us-east-1)
  • AWS_ACCESS_KEY_ID: Access key ID
  • AWS_SECRET_ACCESS_KEY: Secret access key
  • AWS_SESSION_TOKEN: Session token (optional)
  • AWS_PROFILE: Profile name (optional)
  • S3_ENDPOINT: Custom endpoint URL (optional)
  • S3_USE_PATH_STYLE: Use path-style URLs (default: false)
  • S3_TIMEOUT: Operation timeout (default: 30s)
  • S3_CONNECTION_NAME: Connection name (optional)
  • S3_DISABLE_SSL: Disable SSL (default: false)

func (*Config) Clone

func (c *Config) Clone() *Config

Clone creates a deep copy of the configuration.

func (*Config) HasCredentials

func (c *Config) HasCredentials() bool

HasCredentials returns true if explicit credentials are configured.

func (*Config) HasEndpoint

func (c *Config) HasEndpoint() bool

HasEndpoint returns true if a custom endpoint is configured.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid. It returns an error if required fields are missing or invalid.

type CopyObjectInput

type CopyObjectInput struct {
	SourceBucket string
	SourceKey    string
	DestBucket   string
	DestKey      string
	Metadata     map[string]string
}

CopyObjectInput contains the parameters for copying an object.

type CopyObjectOutput

type CopyObjectOutput struct {
	ETag         string
	LastModified time.Time
	VersionID    string
}

CopyObjectOutput contains the result of copying an object.

type ListObjectsOutput

type ListObjectsOutput struct {
	Objects           []ObjectInfo
	CommonPrefixes    []string
	IsTruncated       bool
	NextContinueToken string
	KeyCount          int32
}

ListObjectsOutput contains the result of listing objects.

type ObjectContent

type ObjectContent struct {
	Key          string
	Body         []byte
	ContentType  string
	Size         int64
	LastModified time.Time
	ETag         string
	Metadata     map[string]string
}

ObjectContent contains the content and metadata of an S3 object.

type ObjectInfo

type ObjectInfo struct {
	Key          string
	Size         int64
	LastModified time.Time
	ETag         string
	StorageClass string
}

ObjectInfo contains information about an S3 object.

type ObjectMetadata

type ObjectMetadata struct {
	Key           string
	Size          int64
	LastModified  time.Time
	ETag          string
	ContentType   string
	ContentLength int64
	Metadata      map[string]string
}

ObjectMetadata contains metadata about an S3 object (from HEAD request).

type PresignAPI

type PresignAPI interface {
	PresignGetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
	PresignPutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
}

PresignAPI defines the interface for presigning operations.

type PresignedURL

type PresignedURL struct {
	URL       string
	Method    string
	ExpiresAt time.Time
}

PresignedURL contains information about a presigned URL.

type PutObjectInput

type PutObjectInput struct {
	Bucket      string
	Key         string
	Body        []byte
	ContentType string
	Metadata    map[string]string
}

PutObjectInput contains the parameters for uploading an object.

type PutObjectOutput

type PutObjectOutput struct {
	ETag      string
	VersionID string
}

PutObjectOutput contains the result of uploading an object.

type S3API

type S3API interface {
	ListBuckets(ctx context.Context, params *s3.ListBucketsInput, optFns ...func(*s3.Options)) (*s3.ListBucketsOutput, error)
	ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
	GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
	HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
	PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
	DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
	CopyObject(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options)) (*s3.CopyObjectOutput, error)
}

S3API defines the interface for S3 operations used by Client. This interface enables mocking for unit tests.

Jump to

Keyboard shortcuts

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