/
filesystem.go
53 lines (43 loc) · 1 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"path"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/go-git/go-billy/v5"
)
const (
DefaultSeparator = "/"
)
type S3FS struct {
client *s3.Client
bucket string
root string
separator string
}
// NewS3FS creates a new S3FS Filesystem.
func NewS3FS(client *s3.Client, bucket string) (billy.Filesystem, error) {
// Check for a non-nil client
if client == nil {
return nil, fmt.Errorf("s3 client cannot be nil")
}
return &S3FS{
client: client,
bucket: bucket,
root: "",
separator: DefaultSeparator,
}, nil
}
// Capabilities returns the filesystem capabilities.
func (fs3 *S3FS) Capabilities() billy.Capability {
return billy.ReadCapability | billy.WriteCapability
}
func (fs3 *S3FS) cleanPath(p ...string) string {
// Join the path elements
j := path.Join(p...)
// Clean the path before joining to root
c := path.Clean(j)
// Join the root and cleaned path
f := path.Join(fs3.root, c)
// Return the full path
return path.Clean(f)
}