summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fileinfo.go57
-rw-r--r--fs.go37
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,
+ }
+
+}
diff --git a/fs.go b/fs.go
index 74f0273..18ebb5a 100644
--- a/fs.go
+++ b/fs.go
@@ -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.