First working S3 test

This commit is contained in:
Quentin 2021-04-26 19:46:42 +02:00
parent 96d49a5d89
commit ca13aaf50b
Signed by: quentin
GPG Key ID: A98E9B769E4FF428
2 changed files with 22 additions and 4 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
garage.secret
*.swp
seafile_recovery

View File

@ -6,6 +6,7 @@ import (
"mime" "mime"
"net/url" "net/url"
"path/filepath" "path/filepath"
"strings"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
@ -16,6 +17,7 @@ type S3Walker struct {
config *configCollect config *configCollect
mc *minio.Client mc *minio.Client
bucket string bucket string
pathPrefix string
} }
func NewS3Walker(config *configCollect) *S3Walker { func NewS3Walker(config *configCollect) *S3Walker {
sw := new(S3Walker) sw := new(S3Walker)
@ -28,31 +30,44 @@ func NewS3Walker(config *configCollect) *S3Walker {
log.Fatal(err) log.Fatal(err)
} }
if u.Scheme != "s3" { log.Fatal("URL must be of the following form: s3://ACCESS_KEY:SECRET_KEY@ENDPOINT/BUCKET") } if u.Scheme != "s3" { log.Fatal("URL must be of the following form: s3://ACCESS_KEY:SECRET_KEY@ENDPOINT/REGION/BUCKET[/PREFIX") }
accessKeyID := u.User.Username() accessKeyID := u.User.Username()
secretAccessKey, _ := u.User.Password() secretAccessKey, _ := u.User.Password()
endpoint := u.Host endpoint := u.Host
sw.bucket = u.Path splittedPath := strings.SplitN(u.Path, "/", 4)
if len(splittedPath) < 3 {
log.Fatal("Bucket or region not found")
}
region := splittedPath[1]
sw.bucket = splittedPath[2]
if len(splittedPath) > 3 {
sw.pathPrefix = splittedPath[3]
}
useSSL := true useSSL := true
// Initialize minio client object. // Initialize minio client object.
sw.mc, err = minio.New(endpoint, &minio.Options{ sw.mc, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL, Secure: useSSL,
Region: region,
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
//sw.mc.TraceOn(nil)
return sw return sw
} }
func (sw* S3Walker) onDir(dn *DirNode) { func (sw* S3Walker) onDir(dn *DirNode) {
} }
func (sw* S3Walker) onFile(fn *FileNode) { func (sw* S3Walker) onFile(fn *FileNode) {
fn.Parse() fn.Parse()
contentType := mime.TypeByExtension(filepath.Ext(fn.AbsolutePath)) path := filepath.Join(sw.pathPrefix, fn.AbsolutePath)
info, err := sw.mc.PutObject(sw.ctx, sw.bucket, fn.AbsolutePath, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType }) contentType := mime.TypeByExtension(filepath.Ext(path))
info, err := sw.mc.PutObject(sw.ctx, sw.bucket, path, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType })
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} else if info.Size != int64(fn.Elem.FileSize) { } else if info.Size != int64(fn.Elem.FileSize) {