s3fs

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: MIT Imports: 14 Imported by: 0

README

s3fs

A S3 filesystem implementation of io.Fs.

Supports handling directories and files transparently, while being memory efficient, which allows handles large files without being limited by the available memory.

Install

go get -u github.com/jacoelho/s3fs

Example

package main

import (
	"context"
	"fmt"
	"io/fs"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/s3"
	"github.com/jacoelho/s3fs"
)

func main() {
	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil { panic(err) }

	client := s3.NewFromConfig(cfg, func(opt *s3.Options) {
		opt.UsePathStyle = true
	})

	filesystem := s3fs.New(client, "test")
	data, err := fs.ReadFile(filesystem, "a-file") // not recommend when handling large files
	if err != nil { panic(err) }

	fmt.Println(string(data))
}

Policies

policy resource
s3:ListBucket arn:aws:s3:::YOUR_BUCKET
s3:GetObject arn:aws:s3:::YOUR_BUCKET/YOUR_PREFIX/*
s3:PutObject arn:aws:s3:::YOUR_BUCKET/YOUR_PREFIX/*
s3:DeleteObject arn:aws:s3:::YOUR_BUCKET/YOUR_PREFIX/*
s3:ListMultipartUploadParts arn:aws:s3:::YOUR_BUCKET/YOUR_PREFIX/*
s3:AbortMultipartUpload arn:aws:s3:::YOUR_BUCKET/YOUR_PREFIX/*

License

MIT License

See LICENSE to see the full text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Directory

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

func (*Directory) Close

func (d *Directory) Close() error

func (*Directory) Info

func (d *Directory) Info() (fs.FileInfo, error)

func (*Directory) IsDir

func (d *Directory) IsDir() bool

func (*Directory) Name

func (d *Directory) Name() string

func (*Directory) Read

func (d *Directory) Read(_ []byte) (int, error)

func (*Directory) Stat

func (d *Directory) Stat() (fs.FileInfo, error)

func (*Directory) Type

func (d *Directory) Type() fs.FileMode

type File

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

func (*File) Close

func (f *File) Close() error

Close implements io.Closer interface.

func (*File) Info

func (f *File) Info() (fs.FileInfo, error)

func (*File) IsDir

func (f *File) IsDir() bool

func (*File) Name

func (f *File) Name() string

func (*File) Read

func (f *File) Read(b []byte) (int, error)

func (*File) ReadAt

func (f *File) ReadAt(b []byte, offset int64) (int, error)

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

func (*File) Stat

func (f *File) Stat() (fs.FileInfo, error)

func (*File) Type

func (f *File) Type() fs.FileMode

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

Write implements io.Writer interface.

func (*File) WriteAt

func (f *File) WriteAt(p []byte, off int64) (n int, err error)

WriteAt implements io.WriterAt interface.

type FileInfo

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

func (*FileInfo) Info

func (i *FileInfo) Info() (fs.FileInfo, error)

func (*FileInfo) IsDir

func (i *FileInfo) IsDir() bool

func (*FileInfo) ModTime

func (i *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (i *FileInfo) Mode() fs.FileMode

func (*FileInfo) Name

func (i *FileInfo) Name() string

func (*FileInfo) Size

func (i *FileInfo) Size() int64

func (*FileInfo) Sys

func (i *FileInfo) Sys() interface{}

func (*FileInfo) Type

func (i *FileInfo) Type() fs.FileMode

type Fs

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

Fs is fs.FS S3 filesystem abstraction.

func New

func New(client s3ApiClient, bucket string, opts ...Option) *Fs

New creates a S3 fs abstraction

func (*Fs) Create

func (f *Fs) Create(name string) (*File, error)

Create opens a named file for writing.

func (*Fs) CreateDir

func (f *Fs) CreateDir(name string) (fs.DirEntry, error)

CreateDir creates a name directory Since S3 doesn't have the concept of directories, an empty file .keep is created.

func (*Fs) CreateDirWithContext

func (f *Fs) CreateDirWithContext(ctx context.Context, name string) (fs.DirEntry, error)

CreateDirWithContext creates a name directory Since S3 doesn't have the concept of directories, an empty file .keep is created.

func (*Fs) CreateWithContext

func (f *Fs) CreateWithContext(ctx context.Context, name string) (*File, error)

CreateWithContext opens a named file for writing.

func (*Fs) Open

func (f *Fs) Open(name string) (fs.File, error)

Open opens the named file or directory for reading.

func (*Fs) OpenWithContext

func (f *Fs) OpenWithContext(ctx context.Context, name string) (fs.File, error)

OpenWithContext opens the named file or directory for reading.

func (*Fs) ReadDir

func (f *Fs) ReadDir(dirName string) ([]fs.DirEntry, error)

ReadDir reads the named directory and returns a list of directory entries sorted by filename.

func (*Fs) ReadDirWithContext

func (f *Fs) ReadDirWithContext(ctx context.Context, dirName string) ([]fs.DirEntry, error)

ReadDirWithContext reads the named directory and returns a list of directory entries sorted by filename.

func (*Fs) Remove

func (f *Fs) Remove(filename string) error

Remove removes the named file.

func (*Fs) RemoveDir

func (f *Fs) RemoveDir(name string) error

RemoveDir removes an empty directory.

func (*Fs) RemoveDirWithContext

func (f *Fs) RemoveDirWithContext(ctx context.Context, name string) error

RemoveDirWithContext removes an empty directory.

func (*Fs) RemoveWithContext

func (f *Fs) RemoveWithContext(ctx context.Context, fileName string) error

RemoveWithContext removes the named file.

func (*Fs) Rename

func (f *Fs) Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it.

func (*Fs) RenameWithContext

func (f *Fs) RenameWithContext(ctx context.Context, oldpath, newpath string) error

RenameWithContext renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it.

func (*Fs) Stat

func (f *Fs) Stat(name string) (FileInfo, error)

Stat returns a FileInfo describing the named file.

func (*Fs) StatWithContext

func (f *Fs) StatWithContext(ctx context.Context, name string) (FileInfo, error)

StatWithContext returns a FileInfo describing the named file.

type Option

type Option func(*Fs)

Option is a Fs configuration.

func WithDirectoryFile

func WithDirectoryFile(s string) Option

WithDirectoryFile sets the file created when CreateDir is used.

func WithPartSize

func WithPartSize(size int64) Option

WithPartSize sets the part size used on multipart download or upload.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix defines a common prefix inside a bucket.

func WithTemporaryDirectory

func WithTemporaryDirectory(dirName string) Option

WithTemporaryDirectory sets the temporary directory where the unlinked temporary files will be created.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the timeout when interacting with S3.

Jump to

Keyboard shortcuts

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