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"
"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) {