From ca13aaf50bef520d2a6249c8b063ef1833421ac9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 26 Apr 2021 19:46:42 +0200 Subject: [PATCH] First working S3 test --- .gitignore | 3 +++ s3_walker.go | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c00d17c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +garage.secret +*.swp +seafile_recovery diff --git a/s3_walker.go b/s3_walker.go index aa5eb69..f00ebd4 100644 --- a/s3_walker.go +++ b/s3_walker.go @@ -6,6 +6,7 @@ import ( "mime" "net/url" "path/filepath" + "strings" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" @@ -16,6 +17,7 @@ type S3Walker struct { config *configCollect mc *minio.Client bucket string + pathPrefix string } func NewS3Walker(config *configCollect) *S3Walker { sw := new(S3Walker) @@ -28,31 +30,44 @@ func NewS3Walker(config *configCollect) *S3Walker { 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() secretAccessKey, _ := u.User.Password() 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 // Initialize minio client object. sw.mc, err = minio.New(endpoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Secure: useSSL, + Region: region, }) if err != nil { log.Fatal(err) } + //sw.mc.TraceOn(nil) + return sw } func (sw* S3Walker) onDir(dn *DirNode) { } func (sw* S3Walker) onFile(fn *FileNode) { fn.Parse() - contentType := mime.TypeByExtension(filepath.Ext(fn.AbsolutePath)) - info, err := sw.mc.PutObject(sw.ctx, sw.bucket, fn.AbsolutePath, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType }) + path := filepath.Join(sw.pathPrefix, fn.AbsolutePath) + 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 { log.Fatalln(err) } else if info.Size != int64(fn.Elem.FileSize) {