Browse Source

First working S3 test

master
Quentin 1 year ago
parent
commit
ca13aaf50b
Signed by: quentin GPG Key ID: A98E9B769E4FF428
  1. 3
      .gitignore
  2. 23
      s3_walker.go

3
.gitignore

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

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

Loading…
Cancel
Save