s3

package module
v0.0.0-...-5620d9e Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

S3 Backend for Afero

this module is under development, and not yet production ready. update aws sdk to v2 fix root dir stat

Thanks

The initial code comes from:

Documentation

Overview

Package s3 implements an S3 filesystem using afero

Package s3 implements an S3 filesystem using afero

Package s3 implements an S3 filesystem using afero

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyOpened = errors.New("already opened")

ErrAlreadyOpened is returned when the file is already opened

View Source
var ErrInvalidSeek = errors.New("invalid seek offset")

ErrInvalidSeek is returned when the seek operation is not doable

View Source
var ErrNotImplemented = errors.New("not implemented")

ErrNotImplemented is returned when this operation is not (yet) implemented

View Source
var ErrNotSupported = errors.New("s3 doesn't support this operation")

ErrNotSupported is returned when this operations is not supported by S3

Functions

This section is empty.

Types

type CompletedPart

type CompletedPart struct {
	PartNumber int32
	ETag       string
	Size       int64
}

CompletedPart represents a completed part in multipart upload

type File

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

File represents a file in S3. nolint: govet

func NewFile

func NewFile(fs *Fs, name string) *File

NewFile initializes an File object.

func NewFileWithBucket

func NewFileWithBucket(fs *Fs, name, bucket string) *File

NewFileWithBucket initializes a File object with a specific bucket.

func NewFileWithBucketAndPrefix

func NewFileWithBucketAndPrefix(fs *Fs, name, bucket, rootPrefix string) *File

NewFileWithBucketAndPrefix initializes a File object with a specific bucket and root prefix.

func (*File) Close

func (f *File) Close() error

Close closes the File, rendering it unusable for I/O. It returns an error, if any.

func (*File) Name

func (f *File) Name() string

Name returns the filename, i.e. S3 path without the bucket name.

func (*File) Read

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

Read reads up to len(b) bytes from the File. It returns the number of bytes read and an error, if any. EOF is signaled by a zero count with err set to io.EOF.

func (*File) ReadAt

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

ReadAt reads len(p) bytes from the file starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.

func (*File) Readdir

func (f *File) Readdir(n int) ([]os.FileInfo, error)

Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by ListObjects, in directory order. Subsequent calls on the same file will yield further FileInfos.

If n > 0, Readdir returns at most n FileInfo structures. In this case, if Readdir returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.

If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error.

func (*File) Readdirnames

func (f *File) Readdirnames(n int) ([]string, error)

Readdirnames reads and returns a slice of names from the directory f.

If n > 0, Readdirnames returns at most n names. In this case, if Readdirnames returns an empty slice, it will return a non-nil error explaining why. At the end of a directory, the error is io.EOF.

If n <= 0, Readdirnames returns all the names from the directory in a single slice. In this case, if Readdirnames succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdirnames returns the names read until that point and a non-nil error.

func (*File) Seek

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

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

func (*File) Stat

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

Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.

func (*File) Sync

func (f *File) Sync() error

Sync is a noop.

func (*File) Truncate

func (f *File) Truncate(int64) error

Truncate changes the size of the file. It does not change the I/O offset. If there is an error, it will be of type *PathError.

func (*File) Write

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

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

func (*File) WriteAt

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

WriteAt writes len(p) bytes to the file starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(p).

func (*File) WriteString

func (f *File) WriteString(s string) (int, error)

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

type FileInfo

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

FileInfo implements os.FileInfo for a file in S3.

func NewFileInfo

func NewFileInfo(name string, directory bool, sizeInBytes int64, modTime time.Time) FileInfo

NewFileInfo creates file cachedInfo.

func (FileInfo) IsDir

func (fi FileInfo) IsDir() bool

IsDir provides the abbreviation for Mode().IsDir()

func (FileInfo) ModTime

func (fi FileInfo) ModTime() time.Time

ModTime provides the last modification time.

func (FileInfo) Mode

func (fi FileInfo) Mode() os.FileMode

Mode provides the file mode bits. For a file in S3 this defaults to 664 for files, 775 for directories. In the future this may return differently depending on the permissions available on the bucket.

func (FileInfo) Name

func (fi FileInfo) Name() string

Name provides the base name of the file.

func (FileInfo) Size

func (fi FileInfo) Size() int64

Size provides the length in bytes for a file.

func (FileInfo) Sys

func (fi FileInfo) Sys() interface{}

Sys provides the underlying data source (can return nil)

type Fs

type Fs struct {
	FileProps *UploadedFileProperties // FileProps define the file properties we want to set for all new files
	// contains filtered or unexported fields
}

Fs is an FS object backed by S3.

func NewFs

func NewFs(bucket string, cfg aws.Config) *Fs

NewFs creates a new Fs object writing files to a given S3 bucket. The bucket parameter is the S3 bucket name.

func (Fs) Chmod

func (fs Fs) Chmod(name string, mode os.FileMode, bucket, rootPrefix string) error

Chmod doesn't exists in S3 but could be implemented by analyzing ACLs

func (Fs) Chown

func (Fs) Chown(string, int, int) error

Chown doesn't exist in S3 should probably NOT have been added to afero as it's POSIX-only concept.

func (Fs) Chtimes

func (Fs) Chtimes(string, time.Time, time.Time) error

Chtimes could be implemented if needed, but that would require to override object properties using metadata, which makes it a non-standard solution

func (*Fs) CompleteMultipartUpload

func (fs *Fs) CompleteMultipartUpload(key, uploadID, bucket, rootPrefix string, parts []CompletedPart) error

CompleteMultipartUpload completes the multipart upload with the given parts

func (Fs) Create

func (fs Fs) Create(name, bucket, rootPrefix string) (afero.File, error)

Create a file.

func (*Fs) GetDiskUsage

func (fs *Fs) GetDiskUsage(bucket, prefix string) (int64, error)

GetDiskUsage calculates the total size of all objects under the given prefix. This method uses pagination to efficiently handle large directories.

func (*Fs) InitiateMultipartUpload

func (fs *Fs) InitiateMultipartUpload(key, bucket, rootPrefix string) (string, error)

InitiateMultipartUpload initiates a multipart upload for the given key

func (*Fs) ListBuckets

func (fs *Fs) ListBuckets() ([]string, error)

ListBuckets returns a list of all S3 buckets.

func (Fs) Mkdir

func (fs Fs) Mkdir(name string, perm os.FileMode, bucket, rootPrefix string) error

Mkdir makes a directory in S3.

func (Fs) MkdirAll

func (fs Fs) MkdirAll(path string, perm os.FileMode, bucket, rootPrefix string) error

MkdirAll creates a directory and all parent directories if necessary.

func (Fs) Name

func (Fs) Name() string

Name returns the type of FS object this is: Fs.

func (*Fs) Open

func (fs *Fs) Open(name, bucket, rootPrefix string) (afero.File, error)

Open a file for reading.

func (*Fs) OpenFile

func (fs *Fs) OpenFile(name string, flag int, _ os.FileMode, bucket, rootPrefix string) (afero.File, error)

OpenFile opens a file.

func (Fs) Remove

func (fs Fs) Remove(name, bucket, rootPrefix string) error

Remove a file

func (*Fs) RemoveAll

func (fs *Fs) RemoveAll(name, bucket, rootPrefix string) error

RemoveAll removes a path by listing all objects under the prefix and deleting them in batches. It is much more efficient and reliable on S3 than recursive Readdir + per-file deletes.

func (Fs) Rename

func (fs Fs) Rename(oldname, newname, bucket, rootPrefix string) error

Rename a file. There is no method to directly rename an S3 object, so the Rename will copy the file to an object with the new name and then delete the original.

func (*Fs) S3API

func (fs *Fs) S3API() *s3.Client

S3API returns the underlying S3 client.

func (Fs) Stat

func (fs Fs) Stat(name, bucket, rootPrefix string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *os.PathError.

func (*Fs) UploadPart

func (fs *Fs) UploadPart(key, uploadID, bucket, rootPrefix string, partNumber int32, data []byte) (string, error)

UploadPart uploads a part for the multipart upload

type FsWrapper

type FsWrapper struct {
	Fs         *Fs
	Bucket     string
	RootPrefix string
}

FsWrapper wraps the Fs struct to implement afero.Fs interface with fixed bucket and rootPrefix

func NewFsWrapper

func NewFsWrapper(cfg aws.Config, bucket, rootPrefix string) *FsWrapper

NewFsWrapper creates a new FsWrapper object that implements afero.Fs with fixed bucket and rootPrefix.

func (*FsWrapper) Chmod

func (fw *FsWrapper) Chmod(name string, mode os.FileMode) error

Chmod changes the permissions of a file.

func (*FsWrapper) Chown

func (fw *FsWrapper) Chown(name string, uid, gid int) error

Chown changes the owner and group of a file.

func (*FsWrapper) Chtimes

func (fw *FsWrapper) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times of a file.

func (*FsWrapper) CompleteMultipartUpload

func (fw *FsWrapper) CompleteMultipartUpload(key, uploadID string, parts []CompletedPart) error

CompleteMultipartUpload completes the multipart upload with the given parts.

func (*FsWrapper) Create

func (fw *FsWrapper) Create(name string) (afero.File, error)

Create creates a file.

func (*FsWrapper) InitiateMultipartUpload

func (fw *FsWrapper) InitiateMultipartUpload(key string) (string, error)

InitiateMultipartUpload initiates a multipart upload for the given key.

func (*FsWrapper) ListDirectory

func (fw *FsWrapper) ListDirectory(name string, limit int) ([]os.FileInfo, error)

ListDirectory is an optimized method to list objects in a directory with limit support using the S3 paginator, which is much more efficient than Readdir(-1) for large directories. Parameters:

  • name: directory path
  • limit: maximum number of items to return (0 for default, -1 for no limit)

func (*FsWrapper) ListDirectorySimple

func (fw *FsWrapper) ListDirectorySimple(name string) ([]os.FileInfo, error)

ListDirectorySimple provides backward compatibility with the old signature

func (*FsWrapper) Mkdir

func (fw *FsWrapper) Mkdir(name string, perm os.FileMode) error

Mkdir creates a directory.

func (*FsWrapper) MkdirAll

func (fw *FsWrapper) MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory and all parent directories if necessary.

func (*FsWrapper) Name

func (fw *FsWrapper) Name() string

Name returns the type of FS object this is: Fs.

func (*FsWrapper) Open

func (fw *FsWrapper) Open(name string) (afero.File, error)

Open opens a file for reading.

func (*FsWrapper) OpenFile

func (fw *FsWrapper) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

OpenFile opens a file with specific flags and permissions.

func (*FsWrapper) Remove

func (fw *FsWrapper) Remove(name string) error

Remove removes a file.

func (*FsWrapper) RemoveAll

func (fw *FsWrapper) RemoveAll(path string) error

RemoveAll removes a path and all its contents.

func (*FsWrapper) Rename

func (fw *FsWrapper) Rename(oldname, newname string) error

Rename renames a file.

func (*FsWrapper) Stat

func (fw *FsWrapper) Stat(name string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file.

func (*FsWrapper) UploadPart

func (fw *FsWrapper) UploadPart(key, uploadID string, partNumber int32, data []byte) (string, error)

UploadPart uploads a part for the multipart upload.

type UploadedFileProperties

type UploadedFileProperties struct {
	ACL          *string // ACL defines the right to apply
	CacheControl *string // CacheControl defines the Cache-Control header
	ContentType  *string // ContentType define the Content-Type header
}

UploadedFileProperties defines all the set properties applied to future files

Jump to

Keyboard shortcuts

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