summaryrefslogtreecommitdiff
path: root/fs.go
diff options
context:
space:
mode:
Diffstat (limited to 'fs.go')
-rw-r--r--fs.go27
1 files changed, 26 insertions, 1 deletions
diff --git a/fs.go b/fs.go
index c4d080b..9f5af23 100644
--- a/fs.go
+++ b/fs.go
@@ -3,6 +3,8 @@ package s3
import (
"context"
"errors"
+ "fmt"
+ neturl "net/url"
"os"
"time"
@@ -12,7 +14,8 @@ import (
)
var (
- ErrUnsupported = errors.New("unsupported operation")
+ ErrUnsupported = errors.New("unsupported operation")
+ ErrInvalidProtocol = errors.New("invalid protocol")
)
type Fs struct {
@@ -41,6 +44,28 @@ func NewFs(endpoint, bucket, accessKeyID, secretAccessKey string, useSSL bool) (
}, nil
}
+func NewFsFromURL(url string) (*Fs, error) {
+ r, err := neturl.ParseRequestURI(url)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse url: %w", err)
+ }
+ useSSL := false
+ switch r.Scheme {
+ case "http":
+ case "https":
+ useSSL = true
+ default:
+ return nil, ErrInvalidProtocol
+
+ }
+
+ endpoint := r.Host
+ bucket := r.Path[1:]
+ accessKeyId := r.User.Username()
+ secretAccessKey, _ := r.User.Password()
+ return NewFs(endpoint, bucket, accessKeyId, secretAccessKey, useSSL)
+}
+
// The name of this FileSystem
func (fs *Fs) Name() string {
return "s3"