36 lines
700 B
Go
36 lines
700 B
Go
package s3
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
var Default S3
|
|
|
|
func New(ctx context.Context, uri string) (S3, error) {
|
|
ins, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch ins.Scheme {
|
|
case "http", "https":
|
|
if ins.User == nil {
|
|
return nil, fmt.Errorf("missing access or key")
|
|
}
|
|
access := ins.User.Username()
|
|
key, _ := ins.User.Password()
|
|
return newS3Client(ctx, fmt.Sprintf("%s://%s", ins.Scheme, ins.Host), access, key)
|
|
case "dir":
|
|
return newDirClient(ctx, ins.Host)
|
|
default:
|
|
return nil, fmt.Errorf("invalid new s3 uri scheme: %s", ins.Scheme)
|
|
}
|
|
}
|
|
|
|
func Init(ctx context.Context, uri string) (err error) {
|
|
Default, err = New(ctx, uri)
|
|
return err
|
|
}
|