diff options
| author | Marin Ivanov <[email protected]> | 2022-08-14 20:24:33 +0300 |
|---|---|---|
| committer | Marin Ivanov <[email protected]> | 2022-08-14 20:24:33 +0300 |
| commit | 46ff67ebdcdcc632958a8598ce694b3d3f9056e8 (patch) | |
| tree | 78efa93434a783f24b247d66577bcef1817827b6 | |
| parent | 5c50ab7055c5fb269b197c6d6d23a1108bd17ffe (diff) | |
Add FileInfo transformation from ObjectInfo
| -rw-r--r-- | fileinfo.go | 57 | ||||
| -rw-r--r-- | fs.go | 37 |
2 files changed, 85 insertions, 9 deletions
diff --git a/fileinfo.go b/fileinfo.go new file mode 100644 index 0000000..5c51242 --- /dev/null +++ b/fileinfo.go @@ -0,0 +1,57 @@ +package s3 + +import ( + "io/fs" + "time" + + "github.com/minio/minio-go/v7" +) + +type FileInfo struct { + name string + size int64 + mtime time.Time + + objectInfo *minio.ObjectInfo +} + +// base name of the file +func (fi *FileInfo) Name() string { + return fi.name +} + +// length in bytes for regular files; system-dependent for others +func (fi *FileInfo) Size() int64 { + return fi.size +} + +// file mode bits +func (fi *FileInfo) Mode() fs.FileMode { + return 0o400 // user read/write permissions +} + +// modification time +func (fi *FileInfo) ModTime() time.Time { + return fi.mtime +} + +// abbreviation for Mode().IsDir() +func (fi *FileInfo) IsDir() bool { + return false // S3 has no directories +} + +// underlying data source (can return nil) +func (fi *FileInfo) Sys() interface{} { + return fi.objectInfo +} + +func transformObjectInfo(info minio.ObjectInfo) fs.FileInfo { + return &FileInfo{ + objectInfo: &info, + + name: info.Key, + size: info.Size, + mtime: info.LastModified, + } + +} @@ -1,6 +1,8 @@ package s3 import ( + "context" + "errors" "os" "time" @@ -8,11 +10,19 @@ import ( "github.com/minio/minio-go/v7/pkg/credentials" ) +var ( + ErrUnsupported = errors.New("unsupported operation") +) + type Fs struct { client *minio.Client + bucket string + + // Configurables + Timeout time.Duration } -func NewFs(endpoint, accessKeyID, secretAccessKey string, useSSL bool) (*Fs, error) { +func NewFs(endpoint, bucket, accessKeyID, secretAccessKey string, useSSL bool) (*Fs, error) { // Initialize minio client object. client, err := minio.New(endpoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), @@ -24,9 +34,17 @@ func NewFs(endpoint, accessKeyID, secretAccessKey string, useSSL bool) (*Fs, err return &Fs{ client: client, + bucket: bucket, + + Timeout: 10 * time.Second, }, nil } +// The name of this FileSystem +func (fs *Fs) Name() string { + return "s3" +} + // Create creates a file in the filesystem, returning the file and an // error, if any happens. func (fs *Fs) Create(name string) (File, error) { @@ -36,13 +54,13 @@ func (fs *Fs) Create(name string) (File, error) { // Mkdir creates a directory in the filesystem, return an error if any // happens. func (fs *Fs) Mkdir(name string, perm os.FileMode) error { - panic("not implemented") + return ErrUnsupported } // MkdirAll creates a directory path and all parents that does not exist // yet. func (fs *Fs) MkdirAll(path string, perm os.FileMode) error { - panic("not implemented") + return ErrUnsupported } // Open opens a file, returning it or an error, if any happens. @@ -75,12 +93,13 @@ func (fs *Fs) Rename(oldname, newname string) error { // Stat returns a FileInfo describing the named file, or an error, if any // happens. func (fs *Fs) Stat(name string) (os.FileInfo, error) { - panic("not implemented") -} - -// The name of this FileSystem -func (fs *Fs) Name() string { - return "s3" + ctx, cancel := context.WithTimeout(context.Background(), fs.Timeout) + defer cancel() + info, err := fs.client.StatObject(ctx, fs.bucket, name, minio.GetObjectOptions{}) + if err != nil { + return nil, err + } + return transformObjectInfo(info), nil } // Chmod changes the mode of the named file to mode. |
